.Net 4.0 Threading
& Parallel Programming
         Alex Moore
      Consultant, HMB Inc.
No More Free Lunch


Processors not getting much faster
No More Free Lunch
transistors++
No More Free Lunch
transistors++   speed++
No More Free Lunch
transistors++   speed++
No More Free Lunch
transistors++   speed++


cores++
Elephant in the Room
Elephant in the Room


  Parallel / Concurrent Programming is
         reaching its boiling point
Elephant in the Room


    Can’t ignore it any longer
New in .Net 4.0


  D            PLINQ
  S
  C      Task Parallel Library

      System.Threading
How to Express
  Parallelism
How to Express
      Parallelism


Threading sucks.
How to Express
       Parallelism

Declarative data parallelism - PLINQ
How to Express
       Parallelism

Declarative data parallelism - PLINQ
Imperative data parallelism - Parallel.For
How to Express
       Parallelism

Declarative data parallelism - PLINQ
Imperative data parallelism - Parallel.For
Imperative task parallelism - Tasks
PLINQ

PLINQ provides us with ways to
declaratively partition and merge
our data.
PLINQ

from x in set
where x == somevalue
select expensiveFunction(x);
PLINQ

from x in set
where x == somevalue
select expensiveFunction(x);


from x in set.AsParallel()
where x == somevalue
select expensiveFunction(x);
For && ForEach

Task Parallel Library provides us
automatically parallelizing
versions of For and ForEach.
Parallel.For

for( int i = 0; i < 10; i++ )
{ ... }
Parallel.For

for( int i = 0; i < 10; i++ )
{ ... }



Parallel.For( 0, 10, i =>
{ ... } );
Parallel.ForEach

foreach( var x in set ) { ... }
Parallel.ForEach

foreach( var x in set ) { ... }



Parallel.ForEach( set, x => {...});
Tasks


Tasks allow us to split our
computations into blocks or “tasks”
that can be handled independently.
Tasks

A();
B();
C();

Do_Something();
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();
Task<T>

string a = A();
int    b = B();
double c = C();



Do_Something(a,b,c);
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);
Continuations

String s = A();

Console.WriteLine(s);
Continuations
var t = new Task<string>(
                      ()=> A() );


t.ContinueWith( s =>
         Console.WriteLine(s) );

t.Start;
Concurrent Collections


• Threadsafe collections
• Use lightweight locking
Concurrent Collections

• BlockingCollection<T>
• ConcurrentBag<T>
• ConcurrentDictionary<T>
• ConcurrentQueue<T>
• ConcurrentStack<T>
Demos
Box of Sharp Knives
Box of Sharp Knives




★ Not a silver bullet for performance
Box of Sharp Knives



★ Not a silver bullet for performance
✓ Read, Do, and Learn
Questions?
Comments ?
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
Thanks!

moore.alex@gmail.com
@alexmoore
www.enginerding.com

.Net 4.0 Threading and Parallel Programming

Editor's Notes

  • #11 Data Structures for Coordination - Concurrent Collections - Synchronization Types - Coordination TypesSystem.Linq System.Threading System.Threading.Tasks System.Collections.Concurrent
  • #35 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
  • #36 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
  • #37 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
  • #38 Are there any questions ? Comments ?