2. 2
Introduction
What is parallel programming ?
Why parallel programming ?
The issues that come with parallel
programming ?
How to do parallel programming
in C# ?
Thread
ThreadPool
Task
Parallel
Async
PLinq
How to synchronize in C# ?
Lock
Mutex
SpinLock
Semaphore
ReaderWriterLock
Semaphore Slim
ReaderWriterLockSlim
How to signal in C# ?
Wait / Pulse
Barrier
How to store shared items in C# ?
Wait / Pulse
Barrier
Conclusion
Sommaire
3. 3
What is parallel programming ?
Parallel computing is the simultaneous use of multiple compute resources
Problem Result
Compute resource 1
Compute resource 2
Compute resource m
Sub Problem 1
Sub Problem 2
Sub Problem n
4. 4
Why parallel programming ?
Do the same work but in less time
8 sec
4 sec
Work 1 2 * (0,5 * Work 1)
at the same time
Sequential Parallel
Start
5. 5
Why parallel programming ?
Responsive UI
Long Action
Refresh interface
Refresh interface Refresh interface
Sequential Parallel
See example
Click Click Long Action
6. 6
The issues that come with parallel programming
A is a situation in which two or more competing actions are
each waiting for the other to finish, and thus neither ever does.
Waiting for Task 2 to finish
Task 1
Task 2
End
Waiting for Task 1 to finish
7. 7
The issues that come with parallel programming
A is the behavior of a system where the output is
dependent on the sequence or timing of other uncontrollable events.
Task 1
Task 2
Result 1
Task 1
Task 2
Result 2
Race Condition if
Result 1 != Result 2
8. 8
How to do parallel
programming in C# ?
Thread
ThreadPool
Task
Parallel
Async
PLinq
9. 9
Thread
A is the basic unit to which an operating system allocates
processor time to execute a sequence of instruction.
Process
Thread 1
Instruction 1
Instruction 2
Instruction n
Thread k
Instruction 1
Instruction 2
Instruction n
11. 11
Solutions Thread, ThreadPool, Task, Parallel,
Async, Plinq, …
Differents solutions
ThreadPool : A pool of threads that can be used to execute tasks.
Task : Represents an asynchronous operation.
Parallel : Provides support for parallel loops.
Async : A keyword to specify that a method is asynchronous.
Plinq : Execute LINQ queries in parallel.
18. 18
How to synchronize in C# ?
Lock
Mutex
SpinLock
Semaphore
ReaderWriterLock
Semaphore Slim
ReaderWriterLockSlim
19. 19
Different solutions
Lock
Protect a section of code.
Mutex
Locking across processes.
SpinLock
Locking few instructions (Sum, variable copy, …).
Semaphore
Cap the access of a resource to a fixed number of
thread.
ReaderWriterLock
Control the access of a resource by multiple thread,
allowing 1 writer or many reader at the same time.
Lock
Locking for a long time
can degrade
performances !
Be aware of deadlock !
25. 25
How to signal in C# ?
Reference : http://www.albahari.com/threading/part2.aspx#_Signaling_with_Event_Wait_Handles
Differents solutions
Wait / Pulse methods
Let a thread wait for a signal or send a signal (Pulse).
Barrier
Allow threads to wait for each other.
29. 29
A quick word about atomicity in C#
An atomic operation
An atomic operation cannot be interrupted by other thread.
Example : Incrementing a variable is not atomic
Read the variable
Add 1
Store the result in the same variable
What if in the middle, another thread access this variable ?
Solution
Interlocked.Add, Interlocked.Increment
Interlocked.Exchange, Interlocked.CompareExchange
Interlocked.Read
33. 33
How to store shared items in C# ?
Different Solutions
Standard Collection + Lock
If you only add elements to the collection
Standard Collection + ReaderWriterLockSlim
If you modify & read simultaneously the collection
Native ThreadSafe collections
If you modify & read simultaneously the collection
For highly concurrent scenario
Native ThreadSafe collections
Concurrent collection Non Concurrent equivalent
ConcurrentStack<T> Stack<T>
ConcurrentQueue<T> Queue<T>
ConcurrentDictionary<TKey,TValue> Dictionary<TKey,TValue>
BlockingCollection<T>
ConcurrentBag<T>
35. 35
Parallel programming can help
you to
Compute faster
Create responsive application
Parallel programming has a cost :
Increase code complexity.
Too much synchronization can lead to
worse performance than single
threaded application.
Conclusion
Don’t rush into parallel programming, profile your code if necessary
36. 36
Partenaire Microsoft
MERITIS GOLD PARTNER 2015
Répondre aux attentes de nos
clients et de nos collaborateurs
Améliorer notre expertise sur les
solutions Microsoft
Accompagner nos clients sur ces
domaines technologiques
Être innovant autours des
technologies Microsoft
37. 37
Conseil et Ingénierie en Banque, Finance et Assurance
Siège Social : 14, rue Gaillon – Paris 2ème
Tél. : 01 44 56 88 10 / Fax : 01 44 56 88 11
www.meritis.fr
Merci de votre
attention