I am again here to continue the discussion around Threading. Today we will discuss about Thread Synchronization Fundamentals and related concepts.
In case you didn’t have a look at the previous posts, you can go through them below.
In case you didn’t have a look at the previous posts, you can go through them below.
- Threading Simplified: Part 1 (Threads Inception)
- Threading Simplified: Part 2 (Multithreading Concepts)
- Threading Simplified: Part 3 (Threads type)
- Threading Simplified: Part 4 (Threads Creation)
- Threading Simplified: Part 5 (Thread Pools)
- Threading Simplified: Part 6 (Exception Handling)
- Threading Simplified: Part 7 (Thread Priority)
Let’s start by posing questions to understand the concepts.
Why Synchronization
When working in multithreaded environments, it’s hard to predict which thread is active and executing any piece of code. This becomes even harder when all the threads are executing shared or common code. So to answer, synchronization is required to predict the outcome in multithreaded environment.
What are the ways to handle synchronization?
There are several ways and methods to handle synchronization as in the following:
- Blocking
o Sleep
o Join
o Task.Wait - Locking
o Exclusive (Lock or Monitor, Mutex (Local/ System), Spin Lock)
o Non-exclusive (Semaphore (Local/ System), Semaphore Slim, Reader/Writer Lock) - Signaling
o Event wait handles (AutoResetEvent/ ManualResetEvent)
o Wait/ Pulse
o Countdown event
o Barrier class - Non-blocking
o Memory Barrier
o Volatile Read/ Write classes
o Volatile keyword
o Interlocked class
As you can see thread synchronization has lot of contents and hence we will take them one by one. In this article, we will focus on thread blocking, as it’s easier to understand.
Thread Blocking
The philosophy behind thread blocking is to let another thread wait until current thread is either finished or has consumed a certain amount of time.
The good part of thread blocking is that the waiting thread doesn’t consume any CPU slice until its reactivated back or unblocked.
Thread reactivation or unblocking can also happen using Thread.Interrupt or Thread.Abort methods.
Thread reactivation or unblocking can also happen using Thread.Interrupt or Thread.Abort methods.
Just to add, context switching happens when thread blocks or unblocks that adds an overhead of few microseconds.
Let’s talk about one of the important enum called ThreadState that helps in determining if the current thread is blocked or not.
ThreadState(System.Threading) enum has several values but notably are the following.
- Running
- Background
- Stopped
- Suspended
- Aborted
- WaitSleepJoin
Please note that there is one more ThreadState enum in System.Diagnostics namespace so don’t get confused with that. As per MSDN documentation, these should only be used in debugging scenarios as at times these enums don’t yield correct thread state.
Now let’s see something in action.
Let’s go through the code to understand how constructs of thread blocking really works.
Let’s go through the code to understand how constructs of thread blocking really works.


Prakash TripathiPosted May 3, 2016, 5:03 AM
Thnx Jothimangala.
Jothimangala RamanathanPosted May 3, 2016, 4:55 AM
easy to understand; nicely summarized with examples
Prakash TripathiPosted Mar 6, 2016, 11:03 PM
Thnx Kumaresh.
Prakash TripathiPosted Mar 6, 2016, 11:02 PM
Thnx Karthiga.
Kumaresh RajalingamPosted Mar 6, 2016, 8:29 PM
good one
Kumaresh RajalingamPosted Mar 6, 2016, 8:29 PM
Nice explanation
Sr KarthigaPosted Mar 6, 2016, 8:19 PM
good one
Sr KarthigaPosted Mar 6, 2016, 8:19 PM
Nice explanation
Prakash TripathiPosted Jan 28, 2016, 2:10 AM
Thnx Shubham.
Shubham KumarPosted Jan 28, 2016, 1:20 AM
Very descriptive
Prakash TripathiPosted Jan 27, 2016, 7:04 AM
Thnx Ankit.
Ankit BansalPosted Jan 27, 2016, 6:24 AM
Nice share..thanks..
Prakash TripathiPosted Jan 26, 2016, 9:16 AM
Thnx Debasis.
Debasis SahaPosted Jan 26, 2016, 9:13 AM
Good One..
Prakash TripathiPosted Jan 26, 2016, 7:18 AM
Thnx Ibrahim.
Mohammed IbrahimPosted Jan 26, 2016, 6:51 AM
nice
Santhakumar MunuswamyPosted Jan 26, 2016, 12:55 AM
Thanks for nice article.