Joe Albahari DEV422 Humans cant reliably write freethreaded code Chris Brumme Microsoft If you dont believe writing multithreaded code is hard do everyone a ID: 585386
Download Presentation The PPT/PDF document "Becoming a C# Time Lord" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Becoming a C# Time Lord
Joe Albahari
DEV422Slide2
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 multithreadedcode is like juggling chainsaws
“
”Slide3
Race conditions
Forgotten synchronization
Deadlocks
Wrong granularity
Tearing
Heisenbugs
Priority inversion
Lock convoys
Cascading failure
Silent failureShared writable state…
Weeping AngelsSlide4
Programming concurrency is easy!(
ish
)
Even with shared writable state!Slide5
The problem is one of separationSlide6Slide7
Structured
Parallelism
makes it easy to isolate concurrencySlide8
Data is spread out over
time
rather than
space
.
Work may be
compute-bound
or
I/O-bound.Or both.
What about
unstructured
concurrency?Slide9
Can we separate concurrency in unstructured scenarios?
Yes!
Requires
language supportRequires a mind-shiftSlide10
What is Space Programming?
Assume data is already there, spread out over
space
Think in terms of execution Pull data as needed, ignoring latencySlide11
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!Slide12
First Law of Time:
No toxic long-running methods
Toxic = accessing
shared writable state
Long-running means
>50ms
This means no blockingInstead, program asynchronouslySlide13
What is Asynchronous Programming?
Deals with latency without blocking
An
async method returns quickly to the callerThe method starts
the operation.When complete, it runs a
callback (continuation)Slide14
An asynchronous method
takes responsibility
for its latency
An
adult
in the temporal worldSlide15
How do adults behave?
They
take responsibility
for latencyThey deal properly with failureThey cooperate with other adults
(composition)Slide16
Dealing with Failure
Silent sulking
TantrumSlide17
CompositionSlide18
void
GetWebPageAsync
(
string
uri
,
Action<string> continuation)
{
...
...
}
GetWebPageAsync
(“...”,
Console.WriteLine
);Slide19
Task<string>
GetWebPageAsync
(string
uri
)
{
...
...
}Slide20
Task<
TResult
>
ContinueWith
()
methodResult propertyTask<
TResult>
is a Future
A future is an asynchronous adult!Slide21
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.Slide22
Space
ConcurrencySlide23
SpaceSlide24
Space
(Hard) concurrency
TimeSlide25
Demo
Time without concurrencySlide26Slide27
Second Law of Time:
Isolate Concurrency
Isolate concurrency from both
space
and
time
Favour time over concurrencySlide28
Third Law of Time:
Use
the
Tardis
await
jumps forward in timeProgram the future as though it were present
Preserve pull-based logic & fault handling
The Tardis avoids blockingSlide29
Demo
CompositionSlide30
Fourth Law of Time:
Compose
Apply
composition patterns
to time and concurrency objects
Use
combinators / operatorsSlide31
Four Laws Of Time
No toxic long-running methods
Separate space, time & concurrency
Use the
tardis
(await)
Compose space & time objectsSlide32
Latency is one kind of time problem
What about asynchronous
sequences
?
Naturally lend themselves to push-based approachesSlide33
What do
Timelords
think of events?Slide34Slide35
Demo
HeartbeatSlide36
PUSH
interface
IObservable
<out T> { IDisposable
Subscribe (IObserver<T>);}
interface IObserver<in T>{ void
OnNext (T value); void OnCompleted(); void OnError (Exception error);}
PULLinterface
IEnumerable<out T> {
IEnumerator<T> GetEnumerator();}
interface IEnumerator<out T> : IDisposable
{
bool
MoveNext
();
T Current { get; }
}Slide37
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 TimeSlide38
Give time the same respect as space.Slide39
Related content
Find Me Later At
DevDen
in Expo Hall
1pm today
Download
LINQPad for sample code
www.linqpad.net
Get
the book!C# 5.0 in a Nutshell – Amazon or O’Reilly
Reactive extensions: rx.codeplex.comBrowse/ask questions on
StackOverflowSlide40
Developer Network
Resources for Developers
http://
msdn.microsoft.com
/en-au/
Learning
Virtual Academy
http://www.microsoftvirtualacademy.com/
TechNet
Resources
Sessions on Demand
http://channel9.msdn.com/Events/
TechEd
/Australia/2013
Resources for IT Professionals
http://technet.microsoft.com/en-au/Slide41
© 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.