Download presentation
Presentation is loading. Please wait.
Published byDamian Skinner Modified over 9 years ago
1
Patterns of Parallel Programming with.NET 4 Stephen Toub Principal Architect Parallel Computing Platform Microsoft Corporation stoub@microsoft.com
2
Agenda Delightfully Parallel – Loops – Fork/Join Dependencies – Producer/Consumer – Directed Graphs Speculation Visualizing Anti-patterns (With just an hour, this is a sampling…)
3
Let’s write code!
4
Directed Acyclic Graph (DAG) Example
6
Future of Parallel Programming with.NET vNext Stephen Toub Principal Architect Parallel Computing Platform Microsoft Corporation stoub@microsoft.com
7
Agenda C# and VB Async TPL Dataflow
8
Trends Increasingly connected applications – More latency e.g. everything as a service – More UI responsiveness problems e.g. the toilet bowl of death – More scalability issues Server, Cloud – e.g. streaming data sources, data distribution Client – e.g. modeling interacting entities Async-only APIs – Silverlight
9
.NET Async Patterns Synchronous APM (Begin…, End…) – IAsyncResult, AsyncCallback EAP – Event-based callbacks only // Asynchronous Programming Model [APM] IAsyncResult BeginFoo(..., AsyncCallback callback, object state); TResult EndFoo(IAsyncResult asyncResult); // Event-based Asynchronous Pattern [EAP] class C { public void FooAsync(...[, object userToken]); public event EventHandler FooCompleted; } class FooCompletedEventArgs : AsyncCompletedEventArgs { public TResult Result { get; } } // Synchronous TResult Foo(...);
10
public void CopyStreamToStream(Stream source, Stream destination) { byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = source.Read(buffer, 0, buffer.Length)) != 0) { destination.Write(buffer, 0, numRead); } } This is synchronous code with.NET 4…
11
This is asynchronous (but blocking) code with.NET 4… public Task CopyStreamToStream(Stream source, Stream destination) { return Task.Factory.StartNew(() => { byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = source.Read(buffer, 0, buffer.Length)) != 0) { destination.Write(buffer, 0, numRead); } }); } public void CopyStreamToStream(Stream source, Stream destination) { byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = source.Read(buffer, 0, buffer.Length)) != 0) { destination.Write(buffer, 0, numRead); } }
12
This is asynchronous code with.NET 4… public IAsyncResult BeginCopyStreamToStream( Stream source, Stream destination, AsyncCallback callback, object state) { var tcs = new TaskCompletionSource (state); byte[] buffer = new byte[0x1000]; Action readWriteLoop = null; readWriteLoop = iar => { try { for (bool isRead = iar == null; ; isRead = !isRead) { switch (isRead) { case true: iar = source.BeginRead(buffer, 0, buffer.Length, readResult => { if (readResult.CompletedSynchronously) return; readWriteLoop(readResult); }, null); if (!iar.CompletedSynchronously) return; break; case false: int numRead = source.EndRead(iar); if (numRead == 0) { tcs.TrySetResult(null); if (callback != null) callback(tcs.Task); return; } iar = destination.BeginWrite(buffer, 0, numRead, writeResult => { if (writeResult.CompletedSynchronously) return; destination.EndWrite(writeResult); readWriteLoop(null); }, null); if (!iar.CompletedSynchronously) return; destination.EndWrite(iar); break; } } } catch (Exception e) { tcs.TrySetException(e); if (callback != null) callback(tcs.Task); } }; readWriteLoop(null); return tcs.Task; } public void EndCopyStreamToStream(IAsyncResult asyncResult) { ((Task)asyncResult).Wait(); } public void CopyStreamToStream(Stream source, Stream destination) { byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = source.Read(buffer, 0, buffer.Length)) != 0) { destination.Write(buffer, 0, numRead); } }
13
This is asynchronous code with.NET vNext… public async Task CopyStreamToStreamAsync(Stream source, Stream destination) { byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = await source.ReadAsync(buffer, 0, buffer.Length)) != 0) { await destination.WriteAsync(buffer, 0, numRead); } } public void CopyStreamToStream(Stream source, Stream destination) { byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = source.Read(buffer, 0, buffer.Length)) != 0) { destination.Write(buffer, 0, numRead); } }
14
.NET Async Patterns Synchronous APM (Begin…, End…) – IAsyncResult, AsyncCallback EAP – Event-based callbacks only TAP – Task, Task // Asynchronous Programming Model [APM] IAsyncResult BeginFoo(..., AsyncCallback callback, object state); TResult EndFoo(IAsyncResult asyncResult); // Event-based Asynchronous Pattern [EAP] class C { public void FooAsync(...[, object userToken]); public event EventHandler FooCompleted; } class FooCompletedEventArgs : AsyncCompletedEventArgs { public TResult Result { get; } } // Synchronous TResult Foo(...); // Task-based Asynchronous Pattern Task FooAsync(...);
15
Language – “async” modifier: marks method or lambda as asynchronous – “await” operator: yields control until awaited task completes Framework – Use Task and Task to represent “ongoing operations” Could be async I/O, background work, anything... Single object for status, result, and exceptions Composable callback model – “await” rewrites to use continuations
16
Demos
17
Parallel Extensions in.NET 4
18
TPL Dataflow System.Threading.Tasks.Dataflow.dll Primitives for in-process message passing – Think “buffering + processing” – Built on Tasks, concurrent collections, … – “Dataflow blocks” can be linked together to create networks Based on concepts / designs from – Decades of computer science research / history – Related Microsoft implementations Asynchronous Agents library in Visual C++ 2010 Axum CCR from Microsoft Robotics
19
var c = new ActionBlock (i => { Process(i); }); for(int i=0; i<5; i++) { c.Post(i); } Example: Async Posting ActionBlock Message Queue 0 0 1 1 2 2 3 3 Process(0);Process(1);Process(2);Process(3); 4 4 Process(4);
20
Demos
21
Tasks are the unit of parallelism and asynchrony – Using TPL now will set you up for future success Language support for async programming – Almost as easy as synchronous programming New TPL Dataflow library eases common scenarios – Producer/consumer, asynchronous buffering, … Code samples and CTP bits available from – http://code.msdn.microsoft.com/ParExtSamples http://code.msdn.microsoft.com/ParExtSamples – http://msdn.com/async http://msdn.com/async – http://msdn.com/concurrency http://msdn.com/concurrency
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.