Threading net 4.5
-
Upload
venkatesh-narayanan -
Category
Technology
-
view
597 -
download
3
description
Transcript of Threading net 4.5
.NET Framework 4.5
Async Features
Agenda
• Task Parallel Library• Data Parallelism• Task Parallelism
• Asynchronous Programming with .NET 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.
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.
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.
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
Creating / Running Tasks - Implicitly
• Parallel.Invoke() provides a way to run any number of arbitrary statements concurrently.
• Parallel.Invoke(() => DoSomeWork(), () => DoSomeOtherWork());
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"));
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;
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();
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
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)
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; }
Thread Pool and Work Stealing
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.
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);}
• 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
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
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
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(); }}
Async Features in 4.5
• TPL Performance improvement
• PLINQ – More queries run in parallel
• Coordination Data Structures
• Async Features
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.
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"));}
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.
Visual Studio 2010 Tasks Window
Visual Studio 2010 Parallel Stacks
• With an abundance of tasks, call stack view is not enough!
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
References
• Task Parallel Library - http://msdn.microsoft.com/en-us/library/dd537609.aspx
• http://blogs.msdn.com/b/pfxteam/archive/2010/02/10/9961019.aspx
• Async and Await - http://msdn.microsoft.com/en-us/magazine/hh456403.aspx
• Parallel Programming – http://reedcopsey.com