SlideServe
Menu
Préférences
© SlideServe · v2026

“Parallel Programming with Async and Await ”

talen avatar
18 diapositives · 629 Views · Aug 04, 2014

“Parallel Programming with Async and Await ”. Joe Hummel, PhD joe@joehummel.net Technical Staff: Pluralsight Adjunct Professor: UIC, LUC. http://www.joehummel.net/downloads.html. Agenda. Motivation Execution model Parallel programming with Tasks

“Parallel Programming with Async and Await ” - slide 1
01 / 18“Parallel Programming with Async and Await ”
1 / 17

Télécharger la présentation

“Parallel Programming with Async and Await ”
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. “Parallel Programming with Asyncand Await” Joe Hummel, PhD joe@joehummel.net Technical Staff: Pluralsight Adjunct Professor: UIC, LUC http://www.joehummel.net/downloads.html

  2. Agenda • Motivation • Execution model • Parallel programming with Tasks • Parallel programming with Async / Await • Demos

  3. Async vs. Parallel? • Asyncprogramming: • Better responsiveness… • GUIs (desktop, web, mobile) • Cloud • Windows 8 • Parallel programming: • Better performance… • Engineering • Oil and Gas • Pharma • Science • Social media C C C C C C C C Disk and network I/Otasks number crunching and big data processing

  4. Execution model • Single core: • Multicore: C C Main <<start Work1>> <<start Work2>> if… while… Main <<start Work>> if… while… Work2 Stmt4; Stmt5; Stmt6; Work1 Stmt1; Stmt2; Stmt3; Work Stmt1; Stmt2; Stmt3; C C C C C C Workerthread Workerthread Workerthread Mainthread Mainthread Threads run in parallel Threadsshare, run asynchronously C

  5. Numerous ways to program • Threads (.NET 1.0) • Async Delegates • QueueUserWorkItem • BackgroundWorker • Task Parallel Library (.NET 4.0) • Async / Await (.NET 4.5) Easier…

  6. Demo ― Performance • Mandelbrot set…

  7. Task-based • Programming model based on concept of a Task Task == a unit of work; an object denoting an ongoing operation or computation.

  8. Task-based execution model Parallel.For( ... ); task task task task Windows Process (.NET) App Domain App Domain App Domain C C C C C C C C Task Parallel Library .NET Thread Pool Task Scheduler worker thread worker thread worker thread worker thread Resource Manager Windows

  9. Demo ― Responsiveness • Asian options financial modeling…

  10. Async solution #1: Tasks void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } void button1_Click(…) { varuictx = // 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: ); }

  11. Async solution #2: Async / Await void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } async void button1_Click(…) { var result = awaitTask.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 opas a separate task, and setup a continuation to execute the remaining code when op finishes ― RUNNING ON THE SAME THREAD CONTEXT!

  12. When to use Async / Await? • 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

  13. Observations… • 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

  14. Example • Async web calls are a classic use-case Synchronous Version private byte[] GetURLContents(string url) { varcontent = new MemoryStream(); varwebReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = webReq.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { responseStream.CopyTo(content); } } return content.ToArray(); }

  15. Demo • Asynchronous web requests… • Work bottom-up changing sync calls to async calls • Add await, async, and Task or Task<T> as needed Asynchronous Version private async Task<byte[]>GetURLContentsAsync(string url) { varcontent = new MemoryStream(); varwebReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = await webReq.GetResponseAsync()) { using (Stream responseStream = response.GetResponseStream()) { await responseStream.CopyToAsync(content); } } return content.ToArray(); }

  16. That’s it!

  17. Summary • Thread-based execution model at very bottom • Task-based execution model on top • For Performance: • Prefer Task Parallel Library • For Responsiveness: • Prefer Async / Await

  18. Thank you for attending! • Presenter: Joe Hummel • Email: joe@joehummel.net • Materials: http://www.joehummel.net/downloads.html • For more info: • MSDN Magazine, October 2011 (3 articles): • “Easier Asynchronous Programming with the New Visual Studio AsyncCTP” • “Pause and Play with Await” • “Async Performance: Understanding the Costs of Async and Await”