Click here to Skip to main content
Click here to Skip to main content

Tagged as

Multithreading - Semaphores in C# 4.0

, 26 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Using a semaphore for pooled multithreading in C#.


This article describes using a Semaphore for thread pooling.


A semaphore is an enforcement of size limitation to a thread pool. Once it’s full, no more threads can enter the semaphore until one or more threads complete and hence get terminated. A queue builds up outside for other threads. Then, for each thread that leaves the semaphore, another thread enters from the queue.

A semaphore has no owner. Any thread can release any other thread on a semaphore.

There are two functionally similar versions of this class: Semaphore and SemaphoreSlim. The latter was introduced in Framework 4.0 and has been optimized to meet the low-latency demands of parallel programming. It’s also useful in traditional multithreading because it lets you specify a cancellation token when waiting. It cannot, however, be used for interprocess signaling.

To use a semaphore in C#, you first need to instantiate an instance of a Semaphore object. The basic constructor takes two parameters. The first is the number of resource slots initially available when the object is instantiated. The second parameter is the maximum number of slots available. If you want to reserve some slots for the calling thread, you can do so by making the first parameter smaller than the second. To reserve all slots for new threads, you should make both parameters the same.

Use the Semaphore class to control access to a pool of resources. Threads enter the semaphore by calling the WaitOne method, which is inherited from the WaitHandle class, and release the semaphore by calling the Release method.

The count on a semaphore is decremented each time a thread enters the semaphore, and incremented when a thread releases the semaphore. When the count is zero, subsequent requests block until other threads release the semaphore. When all threads have released the semaphore, the count is at the maximum value specified when the semaphore was created.

Using the Code

In our example code, we are creating a semaphore with an initial size of 3 and a max size of 5, which means initially two slots will be vacant.

using System;
using System.Threading;
namespace Threading
    public class SemaphoreImpl
        // A semaphore that simulates a limited resource pool.
        private static Semaphore semaphore = new Semaphore(3,5);
        //Empty constructor.
        public SemaphoreImpl()
        private void ThreadExcecute(object num)
            //Thread bigins and waiting for slot in semaphore.
            Console.WriteLine("Thread {0} begins " + 
                    "and waits for the semaphore.", num);
            Console.WriteLine("Thread {0} enters the semaphore.", num);
            //In practical scenario should not call sleep. 
            //In our case we are stimulating excecution time of method.
            //Method sucessfully excecuted and thread
            //is supposed to release from semaphore.
            Console.WriteLine("Thread {0} got released from the semaphore.", num);
        public void ExcecuteSemaphore()
            // Create and start five numbered threads. 
            for (int i = 1; i <= 10; i++)
                Thread t = new Thread(new ParameterizedThreadStart(ThreadExcecute));
            Console.WriteLine("Main thread exits.");
            //Wait for some time.

Points of Interest

Thread pooling now becomes efficient.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shine K Velayudhan
Architect Synechron Technologies, Pune
India India
No Biography provided

Comments and Discussions

GeneralMy vote of 1 Pinmemberchait30116-Feb-14 13:26 
GeneralMy vote of 1 PinmemberAlex Blokha29-Oct-13 9:03 
GeneralReason for my vote of 1 You've done nothing more that restat... PinsubeditorWalt Fair, Jr.27-Aug-11 17:27 
GeneralReason for my vote of 1 Copy-Paste from MSDN... Pinmemberazweepay16-Aug-11 23:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 26 Aug 2011
Article Copyright 2011 by Shine K Velayudhan
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid