Joe Hummel, PhD

coleslawokraSoftware and s/w Development

Dec 1, 2013 (3 years and 8 months ago)

83 views

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


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

number crunching and
big data processing

4

Main

thread

Main

<<start Work>>

if…

while…

Task

Stmt1;

Stmt2;

Stmt3;

Main

<<start Work1>>

<<start Work2>>

if…

while…

Task1

Stmt1;

Stmt2;

Stmt3;

Task2

Stmt4;

Stmt5;

Stmt6;

Worker

thread

Worker

thread

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


.NET Thread
Pool

worker

thread

worker

thread

worker

thread

worker

thread

Parallel.For
( ... );

task

task

task

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

result =
DoLongLatencyOp
();



lstBox.Items.Add
(result);

}

11

void
button1_Click
(…)

{



var

result =
DoLongLatencyOp
();



lstBox.Items.Add
(result);

}


async

void button1_Click(…)


{



var

result =
await

Task.Run
(()

=>
DoLongRunningOp
());



lstBox.Items.Add
(result);


}

M
ethod
*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
.


a
wait

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);


}

System.IO.FileStream

file = ...;


int

read =
await

file.ReadAsync
(buffer, offset, count);



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

Asynchronous

System.IO.FileStream

Read

ReadAsync

Write

WriteAsync

CopyTo

CopyToAsync

System.Net.HttpWebRequest

GetResponse

GetResponseAsync

GetRequestStream

GetRequestStreamAsync

System.Net.Http.HttpClient

N/A

GetAsync

N/A

PostAsync,PutAsync,SendAsync


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
();

}

Synchronous
Version

d
ata

=
GetURLContents
(“...”);
// call and prepare to wait:


15


private
async

Task<byte
[]>

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

d
ata

=
await

GetURLContentsAsync
(“...”);
// call, start, don’t wait:


Synchronous
Version


Asynchronous web requests…

16


Return a task for caller to
await

upon…

17

public
int

SomeOperation
(...)

{


int

result;


result = ...;


return result;

}

public
Task<
int
>

SomeOperationAsync
(...)

{



return
Task.Run
<
int
>
( () =>


{



int

result;



result = ...;



return result;



}



);

}

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
:

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