Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Adjunct Professor: U.
Download
Report
Transcript Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Adjunct Professor: U.
Joe Hummel, PhD
Microsoft MVP Visual C++
Technical Staff: Pluralsight, LLC
Adjunct Professor: U. of Illinois, Chicago and
Loyola University Chicago
email: [email protected]
stuff: http://www.joehummel.net/downloads.html
Motivation
Execution model
Parallel programming with Tasks
Parallel programming with Async / Await
Demos
2
Async programming:
Parallel programming:
Better responsiveness…
Better performance…
GUIs (desktop, web, mobile)
Engineering
Cloud
Oil and Gas
Windows 8
Pharma
Science
Social media
Disk
and
network
I/O
number crunching and
big data processing
3
Single core:
Main
Multicore:
Task
<<start Work>>
if…
while…
Main
thread
Stmt1;
Stmt2;
Stmt3;
Worker
thread
Threads
share,
run
asynchr
onously
Main
Task1
<<start Work1>>
<<start Work2>>
if…
while…
Main
thread
Task2
Stmt1;
Stmt2;
Stmt3;
Stmt4;
Stmt5;
Stmt6;
Worker
thread
Worker
thread
Threads run
in parallel
4
Threads
(.NET 1.0)
Async Delegates
QueueUserWorkItem
BackgroundWorker
Task Parallel Library
(.NET 4.0)
Async / Await
(.NET 4.5)
Easier…
5
Mandelbrot set…
6
Programming model based on concept of a Task
Task == a unit of work; an object denoting
an ongoing operation or computation.
7
Parallel.For( ... );
task
tasktasktask
Windows Process (.NET)
App
Domain
App
Domain
App
Domain
Task Parallel Library
worker
thread
worker
thread
worker
thread
worker
thread
Task Scheduler
.NET Thread Pool
global work queue
Resource Manager
Windows
8
Asian options financial modeling…
9
void button1_Click(…)
{
var result = DoLongLatencyOp();
lstBox.Items.Add(result);
}
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:
);
}
10
void button1_Click(…)
{
var result = DoLongLatencyOp();
lstBox.Items.Add(result);
}
async
void button1_Click(…)
{
Method
*may*
perform
async,
longlatency
op
var result =
await
Task.Run(() => DoLongRunningOp());
lstBox.Items.Add(result);
}
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.
11
await must wait on a task
◦ implies underlying method must create & start a task…
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);
12
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
Synchronous
System.IO.FileStream
System.Net.HttpWebRequest
System.Net.Http.HttpClient
Asynchronous
Read
ReadAsync
Write
WriteAsync
CopyTo
CopyToAsync
GetResponse
GetResponseAsync
GetRequestStream
GetRequestStreamAsync
N/A
GetAsync
N/A
PostAsync,PutAsync,SendAsync
Async web calls are a classic use-case
private byte[] GetURLContents(string url)
{
var content = new MemoryStream();
var webReq = (HttpWebRequest)WebRequest.Create(url);
Synchronous
Version
using (WebResponse response = webReq.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
responseStream.CopyTo(content);
}
}
return content.ToArray();
}
data = GetURLContents(“...”);
// call and prepare to wait:
14
private async Task<byte[]> GetURLContentsAsync(string url)
{
var content = new MemoryStream();
var webReq = (HttpWebRequest)WebRequest.Create(url);
Asynchronous
Version
using (WebResponse response = await webReq.GetResponseAsync())
{
using (Stream responseStream = response.GetResponseStream())
{
await responseStream.CopyToAsync(content);
}
}
return content.ToArray();
}
data = await GetURLContentsAsync(“...”);
// call, start, don’t wait:
Synchronous
Version
15
Asynchronous web requests…
16
Return a task for caller to await upon…
public int SomeOperation(...)
{
int result;
result = ...;
return result;
}
public Task<int> SomeOperationAsync(...)
{
return Task.Run<int>( () =>
{
int result;
result = ...;
return result;
}
);
}
17
18
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
Presenter: Joe Hummel
◦ Email:
[email protected]
◦ 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