Download presentation
Presentation is loading. Please wait.
1
DEV422 Becoming a C# Time Lord Joe Albahari 2/5/2019 11:46 AM
© 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
2
“ ” “ ” “ ” Humans can’t reliably write free-threaded code
- Chris Brumme, Microsoft ” “ If you don’t believe writing multithreaded code is hard, do everyone a favor and don’t write any ” “ Writing multithreaded code is like juggling chainsaws ”
3
Race conditions Deadlocks Heisenbugs Wrong granularity Tearing
Forgotten synchronization Priority inversion Heisenbugs Cascading failure Wrong granularity Weeping Angels Race conditions Silent failure Tearing Deadlocks Lock convoys Shared writable state…
4
Programming concurrency is easy!(ish)
Even with shared writable state!
5
The problem is one of separation
7
Structured Parallelism makes it easy to isolate concurrency
8
What about unstructured concurrency?
Data is spread out over time rather than space. Work may be compute-bound or I/O-bound. Or both.
9
Can we separate concurrency in unstructured scenarios?
Yes! Requires language support Requires a mind-shift
10
What is Space Programming?
Assume data is already there, spread out over space Think in terms of execution Pull data as needed, ignoring latency
11
Why is ignoring latency bad?
Causes long-running methods Why are long-running methods bad? Forces caller to initiate thread-based concurrency for responsiveness. Shared writable state then leaks!
12
First Law of Time: No toxic long-running methods
Toxic = accessing shared writable state Long-running means >50ms This means no blocking Instead, program asynchronously
13
What is Asynchronous Programming?
Deals with latency without blocking An async method returns quickly to the caller The method starts the operation. When complete, it runs a callback (continuation)
14
An adult in the temporal world
An asynchronous method takes responsibility for its latency An adult in the temporal world
15
How do adults behave? They take responsibility for latency
They deal properly with failure They cooperate with other adults (composition)
16
Dealing with Failure Silent sulking Tantrum
17
Composition
18
void GetWebPageAsync ( string uri, Action<string> continuation)
{ ... } GetWebPageAsync(“...”, Console.WriteLine);
19
Task<string> GetWebPageAsync (string uri)
{ ... }
20
Task<TResult> is a Future
ContinueWith() method Result property A future is an asynchronous adult!
21
Time vs. concurrency The two are related
When we’re doing concurrency, we’re doing time But we can do time without concurrency? Yes! Time is easier than concurrency.
22
Space Concurrency
23
Space
24
Space (Hard) concurrency Time
25
Demo Time without concurrency 2/5/2019 11:46 AM
© 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
27
Second Law of Time: Isolate Concurrency
Isolate concurrency from both space and time Favour time over concurrency
28
Third Law of Time: Use the Tardis await jumps forward in time
Program the future as though it were present Preserve pull-based logic & fault handling The Tardis avoids blocking
29
Demo Composition 2/5/2019 11:46 AM
© 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
30
Fourth Law of Time: Compose
Apply composition patterns to time and concurrency objects Use combinators / operators
31
Four Laws Of Time No toxic long-running methods
Separate space, time & concurrency Use the tardis (await) Compose space & time objects
32
Latency is one kind of time problem
What about asynchronous sequences? Naturally lend themselves to push-based approaches
33
What do Timelords think of events?
35
2/5/ :46 AM Demo Heartbeat © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
36
PULL PUSH interface IEnumerable<out T> {
IEnumerator<T> GetEnumerator(); } interface IEnumerator<out T> : IDisposable bool MoveNext(); T Current { get; } PUSH interface IObservable<out T> { IDisposable Subscribe (IObserver<T>); } interface IObserver<in T> void OnNext (T value); void OnCompleted(); void OnError (Exception error);
37
Summary Isolation is key to simplifying concurrency
For unstructured concurrency, use futures and observables Futures and observables lessen and isolate concurrency Follow the Four Laws of Time
38
Give time the same respect as space.
39
Related content Find Me Later At DevDen in Expo Hall 1pm today
2/5/ :46 AM Related content Find Me Later At DevDen in Expo Hall 1pm today Download LINQPad for sample code Get the book! C# 5.0 in a Nutshell – Amazon or O’Reilly Reactive extensions: rx.codeplex.com Browse/ask questions on StackOverflow © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
40
Resources Learning TechNet Developer Network
2/5/ :46 AM Resources Learning Sessions on Demand Virtual Academy TechNet Developer Network Resources for IT Professionals Resources for Developers © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
41
2/5/ :46 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION. © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.