290 likes | 410 Views
Parallel Programming in .NET 4.0 Tasks and Threading. Ingo Rammer, thinktecture weblogs.thinktecture.com/ingo @ingorammer. Ingo Rammer and thinktecture. Support and consulting for software architects and developers Architectural Consulting and Prototyping
E N D
Parallel Programming in .NET 4.0 Tasks and Threading Ingo Rammer, thinktecture weblogs.thinktecture.com/ingo@ingorammer
Ingo Rammer and thinktecture • Support and consulting for software architects and developers • Architectural Consulting and Prototyping • Developer-Coaching and -Mentoring • Application Optimization, Troubleshooting, Debugging • Architecture and Code Reviews • Slides/Samples: http://weblogs.thinktecture.com/ingo • ingo.rammer@thinktecture.com
128 cores (Yes, this is a real screenshot)
The future brings ... ... more cores @ lower speed
What do we need? • Parallelism, not just multithreading • Partitioning of work • Queue management • Synchronization • Today • Threads • ThreadPool (==> limited API)
.NET <= 3.5 • Manual management of parallelism • Important • Threads: unit of scheduling, not unit of work! • ThreadPool: limited API
.NET 4.0 • Fine-Grained Parallelism: Task-API and coordination structures (the foundation of it all) • Structured Parallelism: Parallel • Declarative Parallelism: PLINQ • And some underlying optimizations in the ThreadPool
Task API Task t1 = Task.Factory.StartNew(DoSomething); Task t2 = Task.Factory.StartNew(delegate {DoSomething();}); Task t3 = Task.Factory.StartNew(()=>DoSomething()); Something tmp = GetData(); // just dummy parameter Task t4 = Task.Factory.StartNew(p=>((Something)p).DoIt(), tmp); Task t5 = Task.Factory.StartNew(()=>tmp.DoIt()); t1.Wait(); Task.WaitAll(t2,t3,t4); Task.WaitAny(t3,t4,t6);
Tasks with Results Task<int> t1 = Task.Factory.StartNew<int>(() => data.Process()); int val = t1.Result; // blocks until t1 is finished
Task Continuation var firstTask = new Task<int>(() => First()); var secondTask = firstTask.ContinueWith((t) => Second()); firstTask.Start(); secondTask.Wait();
Structured Parallelism • Parallel.Invoke Parallel.Invoke( () => Foo(), () => Bar(), () => Baz());
Structured Parallelism • Parallel.ForEach string[] foo = {"bar","baz","qux"}; Parallel.ForEach(foo, (p) => { DoIt(p); }); // OR ...to support stopping: Parallel.ForEach(foo, (p,s) => { DoIt(p); if (p == "baz") s.Stop(); }); // s is implicitly of type ParallelLoopState
Declarative Parallelism: PLINQ List<DemoData> lst = ...; varlst = from p in lst.AsParallel() where p.Index > 3123 && p.Index < 5892 select p; int sum2 = lst .Where(p=>p.Index>3123 && p.Index<5892) .Sum(p => p.Process());
PLINQ Options lst.AsParallel() .WithDegreeOfParallelism(10) .AsOrdered() .WithMergeOptions(ParallelMergeOptions.FullyBuffered)
Cancellation Support • Unified cancellation with CancellationSource and CancellationToken • Tasks can be manually cancelled, or automatically when parent is cancelled • PLINQ-Queries can specify a CancellationToken CancellationSourcesrc = new CancellationSource(); lst.AsParallel().WithCancellation(src.Token).Sum(...); src.Cancel();
BlockingCollection<T> static BlockingCollection<int> _workItems; void EnqueueProc() { _workItems.Add(123); _workItems.CompleteAdding(); } void ThreadProc() { while (!_workitems.IsCompleted) { var itm = _workitems.Take(); Process(itm); } }
ThreadPool Optimizations • Local queues • Work stealing • Locality by LIFO
What you get with .NET 4.0 • Fine-Grained Parallelism: Task, Task<T> • Structured Parallelism: Parallel.Invoke, Parallel.ForEach • Declarative Parallelism: PLINQ // .AsParallel()
Next steps… Think of possible ways to add parallel capabilities to your existing applications. Otherwise you'll only ever use 1/128th of That Big Machine. • Resources • Daniel Moth's weblog: http://www.danielmoth.com/Blog/ • PFX Team blog: http://blogs.msdn.com/pfxteam/ • Home: http://msdn.microsoft.com/concurrency/
Stay up to date with MSDN Belux • Register for our newsletters and stay up to date:http://www.msdn-newsletters.be • Technical updates • Event announcements and registration • Top downloads • Follow our bloghttp://blogs.msdn.com/belux • Join us on Facebookhttp://www.facebook.com/msdnbehttp://www.facebook.com/msdnbelux • LinkedIn: http://linkd.in/msdnbelux/ • Twitter: @msdnbelux DownloadMSDN/TechNet Desktop Gadgethttp://bit.ly/msdntngadget
TechDays 2011 On-Demand • Watchthis session on-demand via Channel9http://channel9.msdn.com/belux • Download to your favorite MP3 or video player • Get access to slides and recommended resources by the speakers