Professional Documents
Culture Documents
22 Advanced C#
Multithreading
Presented By: John Hinz
@jhinz
Codeinbits.com
© John Hinz – All rights reserved
Agenda
• Introduction to multithreading in .NET
– Process versus thread
– Multi-threading use-cases
– Multi-threading caveats
– Working with threads
• Starting
• Argument passing
• Shutdown coordination
– Working with the thread pool
• Directly
• Asynchronous delegate invocation
• Asynchronous I/O
2
Process Anatomy
3
Processes and Threads
• Threads execute code
– A path of execution through all code within a single process
– Have access to all data within that process
– Each thread has its own stack and a its own copy of CPU registers
– A process with no foreground threads exits – it can no longer
perform work
– A thread ends when the delegate passed to the Thread’s
constructor finishes executing.
4
Why Use Multi-threading?
5
Multi-threading Caveats
6
Task and Data Parallelism
• Namespace System.Threading.Tasks
• Defines static methods for a parallel for and foreach
– Parallel.For
– Parallel.ForEach
• Parallel.Invoke allows you to invoke different methods
concurrently
• Parallel.Invoke is for task parallelism, Parallel.ForEach for
data parallelism
8
Parallel.For Method
• Similar to the to the ‘for loop’ statement but the iterations run in
parallel
– The order of iterations is not defined
• The Parallel.For takes 3 parameters
– First two parameters define the start and end of the loop
– The third parameter is a delegate
• https://dotnetfiddle.net/684FRO
9
Parallel.For Method (cont.)
11
Parallel.Invoke
12
Tasks
• https://dotnetfiddle.net/4wtiAZ
17
Thread Pools
19
Threads (cont.)
21
Background Threads
23
Controlling Threads
24
Controlling Threads (cont)
25
Blocking
26
Threading Issues
• Race Conditions
– Occur if two or more threads access the same objects
– https://dotnetfiddle.net/DlWvPo
– You can avoid the problem by locking the shared object.
– https://dotnetfiddle.net/GSc2CG
• Deadlocks
– In a deadlock, at least two threads halt and wait for each other to release a lock
27
Thread Synchronization
• Lock statement
– The lock statement provides an easy way to hold and release a lock
• Interlocked
– The Interlocked class is used to make simple statements for variables
atomic
• Monitor
– Lock is short form for Monitor. Monitor provides more control
• SpinLock
– useful if you have a large number of locks (for example, for every node
in a list) and hold times are always extremely short
28
Thread Synchronization (cont.)
• WaitHandle
– An abstract base class that you can use to wait for a signal to be set.
• Mutex (mutual exclusion)
– Offers synchronization across multiple processes
• Semaphore
– Similar to a mutex; but unlike the mutex, the semaphore can be used by multiple
threads at once
• Barrier
– For scenarios in which work is forked into multiple tasks and the work must be
joined afterward
• ReaderWriteLockSlim
– Locking mechanism to allow multiple readers, but only one writer, for a resource
29
THANK YOU.
30