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)

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

yolandato4

11/16/2024 11:46:23 PM
Best curvy and chubby pornstars mrporngeek s choice https://anal-penetration-cum-swallowing.fetish-matters.com/?tayler-elise

louellanf6

11/16/2024 4:04:12 PM
Stunning Blonde Kay Lovely Wants to Try Everything Once https://dirty-talk-handjob-germany.fetish-matters.net/?trinity-noemi

natashayn6

11/15/2024 1:49:22 AM
Debunking myths disability sex and love our beautiful baby https://daugter-vintage-retro-sex.fetish-matters.com/?danielle-katie

ceciljb11

11/14/2024 8:38:39 PM
9 best prostate massagers to try for deep intenseasms https://uncut-cock-pussy-to-mouth.fetish-matters.com/?keeley-kenia

veronicaia6

11/14/2024 5:38:25 AM
Phone photos download the best free phone stock photos https://swallowing-cum-selfie.fetish-matters.com/?angeline-arlene

shawnajc10

11/14/2024 3:11:03 AM
K michelle loves white men urges black women to try it out https://french-milf-trailer.fetish-matters.net/?janae-emilee

lesliesr11

11/12/2024 11:26:05 PM
25 of the best lesbian films of all time harper s bazaar https://anal-gape-hairy-pussy-creampie.fetish-matters.com/?reyna-whitney

celinaos18

11/12/2024 7:09:46 PM
Top 10 best onlyfans blowjob girls hot onlyfans bj follows in https://egypt-big-booty-latina.fetish-matters.net/?amber-jennifer

Leave a Comment

All fields are required. Your email address will not be published.