Download presentation
Presentation is loading. Please wait.
Published byStanley Cameron Modified over 9 years ago
1
Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Adjunct Professor: U. of Illinois, Chicago and Loyola University Chicago email:joe@joehummel.net stuff:http://www.joehummel.net/downloads.html
2
Motivation Execution model Parallel programming with Tasks Parallel programming with Async / Await Demos 2
3
3 Async programming: Better responsiveness… GUIs (desktop, web, mobile) Cloud Windows 8 Parallel programming: Better performance… Engineering Oil and Gas Pharma Science Social media Disk and network I/O number crunching and big data processing
4
4 C C C C C C C C Main thread C Main > if… while… Main > if… while… Task Stmt1; Stmt2; Stmt3; Task Stmt1; Stmt2; Stmt3; Main > if… while… Main > if… while… Task1 Stmt1; Stmt2; Stmt3; Task1 Stmt1; Stmt2; Stmt3; Task2 Stmt4; Stmt5; Stmt6; Task2 Stmt4; Stmt5; Stmt6; Worker thread Main thread Threads share, run asynchr onously Threads run in parallel Single core: Multicore:
5
Threads(.NET 1.0) Async Delegates QueueUserWorkItem BackgroundWorker Task Parallel Library(.NET 4.0) Async / Await(.NET 4.5) 5 Easier…
6
Mandelbrot set… 6
7
Programming model based on concept of a Task 7 Task == a unit of work; an object denoting an ongoing operation or computation.
8
8 Windows Process (.NET) App Domain App Domain App Domain App Domain App Domain App Domain.NET Thread Pool worker thread worker thread worker thread worker thread Parallel.For(... ); task global work queue Task Parallel Library Resource Manager Task Scheduler Windows
9
Asian options financial modeling… 9
10
10 void button1_Click(…) { var uictx = // grab UI thread context to run UI task: TaskScheduler.FromCurrentSynchronizationContext(); Task.Factory.StartNew(()=> { return DoLongLatencyOp(); } ).ContinueWith((antecedent) => { lstBox.Items.Add(antecedent.Result); }, uictx // execute this task on UI thread: ); } void button1_Click(…) { var uictx = // grab UI thread context to run UI task: TaskScheduler.FromCurrentSynchronizationContext(); Task.Factory.StartNew(()=> { return DoLongLatencyOp(); } ).ContinueWith((antecedent) => { lstBox.Items.Add(antecedent.Result); }, uictx // execute this task on UI thread: ); } void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); }
11
11 void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } async void button1_Click(…) { var result = await Task.Run(() => DoLongRunningOp()); lstBox.Items.Add(result); } async void button1_Click(…) { var result = await Task.Run(() => DoLongRunningOp()); lstBox.Items.Add(result); } Method *may* perform async, long- latency op Tells compiler to setup a continuation to execute rest of method --- ON SAME THREAD CONTEXT --- and then returns from method so it doesn’t wait.
12
await must wait on a task ◦ implies underlying method must create & start a task… 12 async void button1_Click(…) { var result = await Task.Run(() => DoLongRunningOp()); lstBox.Items.Add(result); } async void button1_Click(…) { var result = await Task.Run(() => DoLongRunningOp()); lstBox.Items.Add(result); } System.IO.FileStream file =...; int read = await file.ReadAsync(buffer, offset, count); System.IO.FileStream file =...; int read = await file.ReadAsync(buffer, offset, count);
13
Think chunky, not chatty ◦ i.e. designed for coarse-grain, long-latency operations ◦ file I/O, network I/O, compute-bound work… Think APIs exposing “TAP” pattern ◦ designed to take advantage of Task-based Asynchronous Pattern
14
Async web calls are a classic use-case 14 private byte[] GetURLContents(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = webReq.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { responseStream.CopyTo(content); } return content.ToArray(); } private byte[] GetURLContents(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = webReq.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { responseStream.CopyTo(content); } return content.ToArray(); } Synchronous Version data = GetURLContents(“...”); // call and prepare to wait:
15
15 private async Task GetURLContentsAsync(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = await webReq.GetResponseAsync()) { using (Stream responseStream = response.GetResponseStream()) { await responseStream.CopyToAsync(content); } return content.ToArray(); } private async Task GetURLContentsAsync(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = await webReq.GetResponseAsync()) { using (Stream responseStream = response.GetResponseStream()) { await responseStream.CopyToAsync(content); } } return content.ToArray(); } Asynchronous Version data = await GetURLContentsAsync(“...”); // call, start, don’t wait: Synchronous Version
16
Asynchronous web requests… 16
17
Return a task for caller to await upon… 17 public int SomeOperation(...) { int result; result =...; return result; } public int SomeOperation(...) { int result; result =...; return result; } public Task SomeOperationAsync(...) { return Task.Run ( () => { int result; result =...; return result; } ); } public Task SomeOperationAsync(...) { return Task.Run ( () => { int result; result =...; return result; } ); }
18
18
19
Thread-based execution model at the bottom Task-based execution model on top For Performance: ◦ Prefer Task Parallel Library For Responsiveness: ◦ Prefer Async / Await 19
20
Presenter: Joe Hummel ◦ Email: joe@joehummel.net ◦ Materials: http://www.joehummel.net/downloads.html For more info: ◦ MSDN Magazine, October 2011 (3 articles): 1.“Easier Asynchronous Programming with the New Visual Studio Async CTP” 2.“Pause and Play with Await” 3.“Async Performance: Understanding the Costs of Async and Await” 20
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.