I am again here to continue the discussion around Threading. Today we will discuss about Semaphore and related concepts. Let’s start by putting some questions to understand the concepts.
- Threading Simplified: Threads Inception - Part 1
- Threading Simplified: Multithreading Concepts - Part 2
- Threading Simplified: Threads type - Part 3
- Threading Simplified: Threads Creation - Part 4
- Threading Simplified: Thread Pools - Part 5
- Threading Simplified: Exception Handling - Part 6
- Threading Simplified: Thread Priority - Part 7
- Threading Simplified: Synchronization Basics and Thread Blocking - Part 8
- Threading Simplified: Thread Locking - Part 9
- Threading Simplified: Monitor - Part 10
- Threading Simplified: Thread Atomicity & Deadlock - Part 11
- Threading Simplified: Mutex - Part 12
What is Semaphore in Multithreading?
Semaphore is yet another synchronization construct in multithreading which is used to limit the number of threads allowed to access any code or logic.
When Semaphore is required how is it different from lock and mutex?
Lock and mutex are used to control synchronization by allowing only one thread at a time to access any resource whereas Semaphore allows more than one thread to access resource or code block.
Also semaphore is thread-independent means any thread can release it unlike lock and mutex where only the owner thread or the thread which obtained them can release.
What is named and unnamed Semaphore?
If Semaphore is named, it can be used to control synchronization across processes in an operating system however unnamed semaphore can only be used across app domains inside a process.
What is SemaphoreSlim?
SemaphoreSlim is an alternative of Semaphore. It is introduced in .NET version 4.0. SemaphoreSlim is a lightweight or slim version of Semaphore and it is recommended to be used when waiting time is expected to be very less. Also SemaphoreSlim can only be used within a single process similar to unnamed Semaphore.
SemaphoreSlim also supports cancellation token that can be used when waiting.
How Semaphore works?
Movie theater analogy can be put to understand working of Semaphore. In Movie theaters there are fix number of seats people can occupy. Once movie is over, another set of audience occupies the same seats.
Semaphore works in the same way. When declaring semaphore, we define the capacity or the number of allowable threads that can enter into code block.
At runtime, we can add the threads into the semaphore till the allowable capacity. Other threads are kept in the queue and enters when existing threads are released.
How to use Semaphore in an application?
Let’s understand this by simple example.
Here we will create Semaphore with three capacity and initial count to start with. We will span five odd threads to execute DoWork method.



Prakash TripathiPosted May 11, 2016, 1:50 AM
Thnx Santosh.
Santosh Kumar AdidawarpuPosted May 11, 2016, 1:44 AM
Nice one..
Prakash TripathiPosted May 10, 2016, 10:15 AM
Thnx Vignesh.
Prakash TripathiPosted May 10, 2016, 10:07 AM
Thnx Sonu.
Vignesh ManiPosted May 10, 2016, 8:32 AM
Nice one
Sonu ChaudharyPosted May 10, 2016, 7:32 AM
good one