Joe Hummel, PhD Technical Staff: Pluralsight Adjunct Professor: UIC, LUC
Motivation Execution model Parallel programming with Tasks Parallel programming with Async / Await Demos 2
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 tasks number crunching and big data processing
4 C C C C C C C C Main thread C Main > if… while… Main > if… while… Work Stmt1; Stmt2; Stmt3; Work Stmt1; Stmt2; Stmt3; Main > if… while… Main > if… while… Work1 Stmt1; Stmt2; Stmt3; Work1 Stmt1; Stmt2; Stmt3; Work2 Stmt4; Stmt5; Stmt6; Work2 Stmt4; Stmt5; Stmt6; Worker thread Main thread Threads share, run asynchr onously Threads run in parallel Single core: Multicore:
Threads(.NET 1.0) Async Delegates QueueUserWorkItem BackgroundWorker Task Parallel Library(.NET 4.0) Async / Await(.NET 4.5) 5 Easier…
Mandelbrot set… 6
Programming model based on concept of a Task 7 Task == a unit of work; an object denoting an ongoing operation or computation.
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
Asian options financial modeling… 9
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 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); } Tells compiler that method *may* perform an async, long-latency op Tells compiler to execute operation but don’t wait. Instead, start op as a separate task, and setup a continuation to execute the remaining code when op finishes ― RUNNING ON THE SAME THREAD CONTEXT!
For operations that may involve long latency ◦ File and network I/O are the classic use-case For chunks of work you want to run in parallel ◦ And you have multi-core hardware 12
Hides the complexities of async programming ◦ No visible callback, predictable exception handling, … Think chunky, not chatty ◦ i.e. designed for coarse-grain work / long-latency operations Designed to be used with APM pattern ◦ Asynchronous Programming Model APIs that offer async calls via APM? ◦ File I/O ◦ Network I/O ◦ Windows 8 API 13
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
Asynchronous web requests… ◦ Work bottom-up changing sync calls to async calls ◦ Add await, async, and Task or Task as needed 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
16
Thread-based execution model at very bottom Task-based execution model on top For Performance: ◦ Prefer Task Parallel Library For Responsiveness: ◦ Prefer Async / Await 17
Presenter: Joe Hummel ◦ ◦ Materials: 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” 18