Multithreading: using Semaphores

In the previous post we’ve seen several concepts associated with semaphore usage. In this post we’re going to see some code that shows how to use this synchronization kernel object. Creating a new semaphore is accomplished through one of the existing constructors:

public Semaphore(int initialCount, int maximumCount);
public Semaphore(int initialCount, int maximumCount, string name);
public Semaphore(int initialCount, int maximumCount, string name, out bool createdNew);
public Semaphore(int initialCount, int maximumCount, string name, out bool createdNew, SemaphoreSecurity semaphoreSecurity);

Whenever you create a semaphore, you need to initialize its initial and maximum allowed count. These values might be a little confusing,so I think it would be wise to describe them a little better before going on…initialCount is used for initializing the internal counter used by the sempahore. On the other hand,maximumCount defines the maximum number of threads that can acquire the resource protected by the semaphore without blocking (after maximumCount has been reached, all threads will block whenever they try to acquire the protected resource). Passing different values for these properties is the same thing as having the current thread call WaitOne maximumCount-initialCount times. Take a look at the following instantiations:

(1) var semaphore = new Semaphore(0, 2);
(2) var semaphore = new Semaphore(2, 2);

On (1), the next thread that performs a take (ie, that calls WaitOne) over the semaphore will block; on the other hand, (2) lets you call WaitOne twice before blocking the thread that performs the next call.

Since there isn’t the concept of ownership, nothing prevents you from calling Release from a thread that didn’t acquire the semaphore. You can even use the overload that accepts an integer that indicates the number of times you’re releasing the semaphore. In practice, this means that Release bumps up the internal counter, allowing other threads that were blocked to awake up. You need to be sure that you don’t go over the maximum value allowed (passed to the semaphore through the constructor) or you’ll get an exception.

Everything we’ve said about *named* mutexes is also true for semaphores (ie, when you create a named semaphore, you might be getting a new semaphore or an existing semaphore – you need to check the createdNew parameter to see what has happened after that call). Ok, I guess it’s time to take a look at some code. The following is a possible implementation of a concurrent stack where we allow several consumers and producers to add and remove elements concurrently from the queue. Here is the code*:

class ConcurrentStack<T> {
    private Stack<T> _stack = new Stack<T>();
    private Mutex _lock = new Mutex();
    private Semaphore _producer;
    private Semaphore _consumer;
    public ConcurrentStack(Int32 maxCapacity) {
        _producer = new Semaphore(maxCapacity, maxCapacity);
        _consumer = new Semaphore(0, maxCapacity);
    public T Pop() {
        T element;
        try {
            element = _stack.Pop();
        finally {
        return element;
    public void Push(T element) {
        try {
        finally {

As you can see, we’re not using the semaphore for achieving mutual exclusion (that’s the job of the mutex, which is there for ensuring proper mutual exclusion for the queue access). In this case, we’re using semaphores for controlling the number of pops and pushes that can happen at a specific time. As you can see, there are two semaphores: one for consumers (which control the number of concurrent pop instructions that can happened) and another for producers (that control entry on the Push method). The previous code ensures that a thread that tries to add an item to a stack that has maxCapacity elements is blocked (the same happens for a thread that tries to remove an element from an empty stack).

Notice that acquiring and releasing operations are performed in different methods (compare this behavior with the mutex’s behavior!). Whenever we pop an element, we take one from the consumer’s semaphore and then we call Release over the producer’s semaphore, leading to a possible wake of a thread that might have been previously blocked. The inverse principle is followed by the Pop method.

Now we can simply create several consumer and producer threads. Here’s some code that does that:

var stack = new ConcurrentStack<Int32>(2);
var consumerThread1 = new Thread( () => { Console.WriteLine( stack.Pop() ); });
var consumerThread2 = new Thread( () => { Console.WriteLine( stack.Pop() ); });
var producerThread1 = new Thread(num => { stack.Push((Int32)num);});
var producerThread2 = new Thread(num => { stack.Push((Int32)num); });

If you run this program, you’ll notice that the consumers will block until there are elements on the stack. I think that this simple example is good for showing the things at which semaphores are good at. And that’s it for today. On the next post, we’ll be talking about auto and reset events. Keep tuned!

* heavily influenced by Joe Duffy’s Concurrent Programming on Windows excellent book.


~ by Luis Abreu on May 17, 2009.

5 Responses to “Multithreading: using Semaphores”

  1. Hey guys,

    I found a website that actually worked to get a free IPod Touch! It should work for you too, it takes a few weeks to get it but hey it”s free! – Click Here to get yours.


    Attention All Music Lovers

    Face it – & Amazon are tooooo Darn Expensive

    Just a short note to let you know that Buy MP3 Music website has re-done their website and are now offering music at 14 cents per song.

    Purchase & Download Full Albums of Michael Jackson, Eminem, Linkin Park, Madonna, etc., for just $1 dollar. It makes no sense to pay Itunes $1 per song when you can get the full album!

    Advantanges of Mp3 Download Portal
    ** Listen to 30 Second Sample of Music Before Purchase.
    ** All Music is in MP3 Format – NO DRM. Play on Ipods & Iphones!
    ** Instant Download Favorite Mp3 Music
    ** Top 10 Music for Free just for Registering.
    ** All Music Costs 14Cents Per Song (Itunes cost over $1)

  3. Hey People

    I”m looking to learn Krav Maga. I have some MMA experience and I”m wondering if Krav Maga is right for me. I”m looking for a mix of Self Defense and a Krav Maga Workout

    I need some expert advice here



  4. excellent example , thanks

  5. Hello everyone.

    My name is Jonathan, currently based in belgium,
    I am both English and French native language,
    just wanted to say hello and Looking forward to sharing information with all of you:-)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: