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

Multithreading - Semaphores in C# 4.0

By , 26 Aug 2011
 

Introduction

This article describes using a Semaphore for thread pooling.

Background

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);
            semaphore.WaitOne();
 
            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.
            Thread.Sleep(1000);
 
            //Method sucessfully excecuted and thread
            //is supposed to release from semaphore.
            Console.WriteLine("Thread {0} got released from the semaphore.", num);
            semaphore.Release();
        }
 
        public void ExcecuteSemaphore()
        {
            // Create and start five numbered threads. 
            for (int i = 1; i <= 10; i++)
            {
                Thread t = new Thread(new ParameterizedThreadStart(ThreadExcecute));
                t.Start(i);
            }
 
            Console.WriteLine("Main thread exits.");
            //Wait for some time.
            Thread.Sleep(500);
            
        }
    }
}

Points of Interest

Thread pooling now becomes efficient.

License

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
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 1 You've done nothing more that restat...subeditorWalt Fair, Jr.27 Aug '11 - 16:27 
GeneralReason for my vote of 1 Copy-Paste from MSDN...memberazweepay16 Aug '11 - 22:33 

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

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