Threading net 4.5

29
.NET Framework 4.5 Async Features

description

Introduction to new async features in .NET Framework 4.5

Transcript of Threading net 4.5

Page 1: Threading net 4.5

.NET Framework 4.5

Async Features

Page 2: Threading net 4.5

Agenda

• Task Parallel Library• Data Parallelism• Task Parallelism

• Asynchronous Programming with .NET 5

Page 3: Threading net 4.5
Page 4: Threading net 4.5

What is the Task Parallel Library(TPL)?

• Set of public types and APIs in .NET version 4 Framework

• These APIs lie in the System.Threading and System.Threading.Task namespaces

• These APIs scale the degree of concurrency as needed to effectively use resources available.

• API supports Task and Data Parallelism.

• Task parallelism focuses on distributing the code execution across different parallel computing nodes(cores).

• Data parallelism focuses on distributing the data across different parallel computing nodes.

Page 5: Threading net 4.5

Data Parallelism

• Same operation concurrently on elements in source collection or array.

• In data parallel operations, the source collection is partitioned so that multiple threads can operate on different segments concurrently.

• TPL supports data parallelism through the System.Threading.Tasks.Parallel class.

• More details on Data Parallelism here.

Page 6: Threading net 4.5

Task Parallellism

• Task Parallel Library (TPL) – Based on concept of task

• What is Task ? Task represents any asynchronous operation.

• Task Parallel refers to one or more tasks running concurrently.

• Task provides two benefits:• More efficient and more scalable use of system

resources.• More programmatic control than is possible with thread

or work item.

Page 7: Threading net 4.5

Task Parallel Library – Tasks

• System.Threading.Tasks• Task• Parent-child relationships• Explicit grouping• Waiting and cancelation

• Task<T>• Tasks that produce values• Also known as futures

ParallelTask

1Task

2

Task N

Page 8: Threading net 4.5

Creating / Running Tasks - Implicitly

• Parallel.Invoke() provides a way to run any number of arbitrary statements concurrently.

• Parallel.Invoke(() => DoSomeWork(), () => DoSomeOtherWork());

Page 9: Threading net 4.5

Creating / Running Tasks - Explicitly

• Tasks class – System.Threading.Tasks.Task

• Every task has a TaskStatus – Can be running, cancelled, faulted.

• var taskA = new Task(() => Console.WriteLine("Hello from Task A"));

taskA.Start();

• var taskB = Task.Factory.StartNew(() => Console.WriteLine("Hello from Task B"));

Page 10: Threading net 4.5

Return a Value from a Task

• Task<Result> should be used for tasks that return a value.

• If Task.Result is accessed before completion, property will block thread until the value is available.

Task<Test> task2 = Task<Test>.Factory.StartNew(() => {

string s = ".NET"; double d = 4.0; return new Test { Name = s, Number = d }; }); Test test = task2.Result;

Page 11: Threading net 4.5

Creating Task Continuations

• Task.ContinueWith allow to specify a task to be started when the antecedent task completes.

• The continuation task delegate is passed a reference to the antecedent.

Task<byte[]> getData = new Task<byte[]>(() => GetFileData()); Task<double[]> analyzeData = getData.ContinueWith(x => Analyze(x.Result)); Task<string> reportData = analyzeData.ContinueWith(y => Summarize(y.Result)); getData.Start();

Page 12: Threading net 4.5

Nested Tasks vs Child Tasks

• Nested Tasks – Creating a new nested task that is not synchronized with the outer task.

• Child Tasks – A new task is created with the option AttachToParent

Page 13: Threading net 4.5

Task Cancellations

• Support for cancelling tasks using cancellation tokens.

• Cancellation involves co-operation between user delegate and calling code.

• Trigger cancellation using CancellationTokenSource.Cancel.

• public Task<int> ReadAsync(byte [] buffer, int offset, int count, CancellationToken cancellationToken)

Page 14: Threading net 4.5

Progress Reporting• Support for asynchronous operations with progress

notifications.

• In TAP, progress reporting is handled through IProgress<T>.

• public Task<int> ReadAsync(byte [] buffer, int offset, int count,

IProgress<int> progress);

public class Progress<T> : IProgress<T>{ public Progress(); public Progress(Action<T> handler); protected virtual void OnReport(T value); public event EventHandler<T> ProgressChanged; }

Page 15: Threading net 4.5

Thread Pool and Work Stealing

Page 16: Threading net 4.5

Design Guidelines for TAP

• Both compute bound and I/O bound operations can be supported using TAP.

• In any public library only I/O bound operations should be exposed as TAP implementation.

• For any compute bound operations expose it as synchronous implementation. Let the caller wrap the invocation into a Task.

Page 17: Threading net 4.5

Compute Bound Task - Examplepublic Task<Bitmap> RenderAsync( ImageData data, CancellationToken cancellationToken){ return Task.Run(() => { var bmp = new Bitmap(data.Width, data.Height); for(int y=0; y<data.Height; y++) { cancellationToken.ThrowIfCancellationRequested(); for(int x=0; x<data.Width; x++) { … // render pixel [x,y] into bmp } } return bmp; }, cancellationToken);}

Page 18: Threading net 4.5

• Enable LINQ developers to leverage parallel hardware• Fully supports all .NET Standard Query Operators• Abstracts away the hard work of using parallelism

• Partitions and merges data intelligently (classic data parallelism)

• Minimal impact to existing LINQ programming model• AsParallel extension method

• Optional preservation of input ordering (AsOrdered)• Query syntax enables runtime to auto-parallelize

• Automatic way to generate more Tasks, like Parallel• Graph analysis determines how to do it• Very little synchronization internally: highly efficient

Parallel LINQ (PLINQ)

var q = from p in people        where p.Name == queryInfo.Name && p.State == queryInfo.State && p.Year >= yearStart && p.Year <= yearEnd        orderby p.Year ascending        select p;

.AsParallel() Query

Task 1

Task N

Page 19: Threading net 4.5

APM and EAP

• Common Concurrency patterns in .NET

• Beginxxx – Kicks of an asynchronous operation and returns IAsyncResult

• Endxxx – Accepts IAsyncResult and returns completed value.

• Future<T> and Task implements IAsyncResult

Page 20: Threading net 4.5

Coordination Data Structures

• New namespace System.Collections.Concurrent

• Avoids locking where possible and uses fine grained locks when locks are necessity.

• User code does not require to take any locks

• ConcurrentQueue, ConcurrentStack, ConcurrentDictionary, ConcurrentBag, BlockingCollection (IProducerConsumerCollection)

• New Synchronization Primitives

Page 21: Threading net 4.5

Lock Free Updatesint x; void MultiplyXBy (int factor){ var spinWait = new SpinWait(); while (true) { int snapshot1 = x; int calc = snapshot1 * factor; int snapshot2 = Interlocked.CompareExchange (ref x, calc, snapshot1); if (snapshot1 == snapshot2) return; // No one preempted us. spinWait.SpinOnce(); }}

Page 22: Threading net 4.5

Async Features in 4.5

• TPL Performance improvement

• PLINQ – More queries run in parallel

• Coordination Data Structures

• Async Features

Page 23: Threading net 4.5

Async and Await

• All asynchronous methods will work via a method that returns Task or Task<T>.

• New Async keyword in the language. Used to indicate an asynchronous function that returns Task or Task<T>.

• Any asynchronous function there should be atleast one await expression.

Page 24: Threading net 4.5

Download Uri’s – Async Example

private async void button1_Click(object sender, RoutedEventArgs e){ string url = "http://reedcopsey.com"; string content = await new WebClient().DownloadStringTaskAsync(url); this.textBox1.Text = string.Format("Page {0} supports XHTML 1.0: {1}", url, content.Contains("XHTML 1.0"));}

Page 25: Threading net 4.5

Await

• At API level the way to achieve waiting without blocking is to provide callbacks.

• Await is Language based asynchrony.

• Hides callbacks by allowing asynchronous operations to be awaited within normal control flow.

• Under the covers, the await functionality installs a callback on the task via a continuation.

Page 26: Threading net 4.5

Visual Studio 2010 Tasks Window

Page 27: Threading net 4.5

Visual Studio 2010 Parallel Stacks

• With an abundance of tasks, call stack view is not enough!

Page 28: Threading net 4.5

Summary

• Exit threads; enter tasks• Only parallel applications will survive• CPU-bound: TPL, PLINQ• I/O-bound: APM

• Shared state is a key problem• Patterns and frameworks are emerging• Tooling support