Successfully reported this slideshow.
Your SlideShare is downloading. ×

.Net 4.0 Threading and Parallel Programming

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad

Check these out next

1 of 39 Ad
Advertisement

More Related Content

Slideshows for you (20)

Viewers also liked (20)

Advertisement

Similar to .Net 4.0 Threading and Parallel Programming (20)

Recently uploaded (20)

Advertisement

.Net 4.0 Threading and Parallel Programming

  1. 1. .Net 4.0 Threading & Parallel Programming Alex Moore Consultant, HMB Inc.
  2. 2. No More Free Lunch Processors not getting much faster
  3. 3. No More Free Lunch transistors++
  4. 4. No More Free Lunch transistors++ speed++
  5. 5. No More Free Lunch transistors++ speed++
  6. 6. No More Free Lunch transistors++ speed++ cores++
  7. 7. Elephant in the Room
  8. 8. Elephant in the Room Parallel / Concurrent Programming is reaching its boiling point
  9. 9. Elephant in the Room Can’t ignore it any longer
  10. 10. New in .Net 4.0 D PLINQ S C Task Parallel Library System.Threading
  11. 11. How to Express Parallelism
  12. 12. How to Express Parallelism Threading sucks.
  13. 13. How to Express Parallelism Declarative data parallelism - PLINQ
  14. 14. How to Express Parallelism Declarative data parallelism - PLINQ Imperative data parallelism - Parallel.For
  15. 15. How to Express Parallelism Declarative data parallelism - PLINQ Imperative data parallelism - Parallel.For Imperative task parallelism - Tasks
  16. 16. PLINQ PLINQ provides us with ways to declaratively partition and merge our data.
  17. 17. PLINQ from x in set where x == somevalue select expensiveFunction(x);
  18. 18. PLINQ from x in set where x == somevalue select expensiveFunction(x); from x in set.AsParallel() where x == somevalue select expensiveFunction(x);
  19. 19. For && ForEach Task Parallel Library provides us automatically parallelizing versions of For and ForEach.
  20. 20. Parallel.For for( int i = 0; i < 10; i++ ) { ... }
  21. 21. Parallel.For for( int i = 0; i < 10; i++ ) { ... } Parallel.For( 0, 10, i => { ... } );
  22. 22. Parallel.ForEach foreach( var x in set ) { ... }
  23. 23. Parallel.ForEach foreach( var x in set ) { ... } Parallel.ForEach( set, x => {...});
  24. 24. Tasks Tasks allow us to split our computations into blocks or “tasks” that can be handled independently.
  25. 25. Tasks A(); B(); C(); Do_Something();
  26. 26. Tasks Task t1 = Task.Factory.StartNew( ()=> A(); ); Task t2 = Task.Factory.StartNew( ()=> B(); ); Task t3 = Task.Factory.StartNew( ()=> C(); ); Task.WaitAll(t1, t2, t3); Do_Something();
  27. 27. Task<T> string a = A(); int b = B(); double c = C(); Do_Something(a,b,c);
  28. 28. Task<T> Task<string> a = Task<string>.Factory.StartNew( ()=> A(); ); Task<int> b = Task<int>.Factory.StartNew( ()=> B(); ); Task<double> c = Task<double>.Factory.StartNew( ()=> C(); ); Do_Something(a.Result,b.Result,c.Result);
  29. 29. Continuations String s = A(); Console.WriteLine(s);
  30. 30. Continuations var t = new Task<string>( ()=> A() ); t.ContinueWith( s => Console.WriteLine(s) ); t.Start;
  31. 31. Concurrent Collections • Threadsafe collections • Use lightweight locking
  32. 32. Concurrent Collections • BlockingCollection<T> • ConcurrentBag<T> • ConcurrentDictionary<T> • ConcurrentQueue<T> • ConcurrentStack<T>
  33. 33. Demos
  34. 34. Box of Sharp Knives
  35. 35. Box of Sharp Knives ★ Not a silver bullet for performance
  36. 36. Box of Sharp Knives ★ Not a silver bullet for performance ✓ Read, Do, and Learn
  37. 37. Questions? Comments ?
  38. 38. How to find info • http://msdn.microsoft.com/en-us/library/ dd460693.aspx • http://code.msdn.microsoft.com/ParExtSamples • Concurrent Programming on Windows - Joe Duffy • Patterns of Parallel Programming - Stephen Toub
  39. 39. Thanks! moore.alex@gmail.com @alexmoore www.enginerding.com

Editor's Notes



















  • Data Structures for Coordination
    - Concurrent Collections
    - Synchronization Types
    - Coordination TypesSystem.Linq
    System.Threading
    System.Threading.Tasks
    System.Collections.Concurrent



















































  • This should not be your first choice for improving performance. Clean &amp;#x201C;responsible&amp;#x201D; design will go a long way when optimizing code, and will help when deciding where to apply concurrency.



    Photo: http://www.discountcutlery.net/en-us/dept_24090.html
  • This should not be your first choice for improving performance. Clean &amp;#x201C;responsible&amp;#x201D; design will go a long way when optimizing code, and will help when deciding where to apply concurrency.



    Photo: http://www.discountcutlery.net/en-us/dept_24090.html
  • This should not be your first choice for improving performance. Clean &amp;#x201C;responsible&amp;#x201D; design will go a long way when optimizing code, and will help when deciding where to apply concurrency.



    Photo: http://www.discountcutlery.net/en-us/dept_24090.html
  • Are there any questions ? Comments ?




×