How to Implement Thread Synchronization using Semaphore

Semaphore is mainly used in scenarios where we have a limited number of resources and we have to limit the number of threads that can use it. The semaphore class lets you limit the number of threads accessing a critical section.

Semaphores are Int32 variables stored in an operating system's resources. When we initialize the semaphore object, we initialize it with a number. This number limits the threads that can enter the critical section.

For using a semaphore in C#, you just need to instantiate an instance of a Semaphore object. 

public static class SemaphoreSample
{
    public static void Execute()
    {
        for (int i = 0; i < 5; i++)
        {
            Thread doWorkThread = new Thread(new ParameterizedThreadStart(doWork));

            doWorkThread.Start(i);
        }

        Console.ReadLine();
    }

    //A semaphore is very similar to lock/Monitor. The difference is that it can allow for a specified amount of threads to work with the same resources at the same time.
    //Make a semaphore that allows for up to two threads to work with the same resource. 
    private static Semaphore semaphore = new Semaphore(1, 1); //Semaphore(initial thread count, max thread count)

    private static void doWork(object threadNumber)
    {
        try
        {
            //The code will stop executing here and wait until the lock is released by the previous thread. 
            semaphore.WaitOne();

            Console.WriteLine("Thread " + threadNumber.ToString() + " has started.");

            //Simulate doing time consuming work.
            Thread.Sleep(1500);

            Console.WriteLine("Thread " + threadNumber.ToString() + " is done.");
        }
        finally
        {
            //Realesase the lock so other threads can now get access.
            semaphore.Release();
        }
    }
}
Semaphore

Comments (10)

margueritewt5

11/29/2024 8:33:11 AM
5 best iphone backup software for pc the windows club https://tickling-close-up-anal.fetish-matters.net/?meghan-natalia

yvetteqr2

11/28/2024 9:24:35 AM
REZERO Rem and Ram Have fun with Strapon https://soldier-anal-queen.fetish-matters.com/?kallie-tamara

marahh9

11/27/2024 6:39:40 PM
Best curvy and chubby pornstars mrporngeek s choice https://halloween-costume-nyseed.fetish-matters.com/?jessie-yessenia

shaneyf2

11/27/2024 12:11:06 PM
The best trans porn sites on the internet trans ecstasy https://hard-dirtyflix.fetish-matters.net/?tyler-stephany

soniang4

11/27/2024 1:57:41 AM
Delightful married newcomer to bestiality gets her sore pussy https://fortnite-porn-cougars.fetish-matters.net/?amanda-kennedi

claudiadf9

11/26/2024 7:31:06 AM
The top 18 best and hottest pornstars of 2024 free videos https://hot-gay-hubby.fetish-matters.net/?anne-keeley

patricekx11

11/25/2024 11:35:11 PM
The 10 best cunnilingus positions for oral sex lovers everywhere sheknows https://gay-boy-sex-wet-pussy-fingering.fetish-matters.net/?ella-joana

marvined7

11/25/2024 2:53:22 AM
The best erotic movies with explicit sex erotismo sexual https://pierced-nipple-preggo.fetish-matters.com/?mallory-jaime

luishh2

11/18/2024 8:10:56 AM
Pornstars hd videos most beautiful and famous porn actresses https://cuatro-patas-stepson.fetish-matters.net/?aubrie-janie

ericqg60

11/18/2024 5:41:58 AM
Enjoy the internet s hottest sex chat for free isexychat https://bubble-butt-teen-all-natural.fetish-matters.net/?jadyn-jaylyn