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 (7)

lindale69

4/19/2024 10:40:49 AM
Swallow hd videos good girls that love to swallow fresh cum https://thief-gays-bareback.fetish-matters.com/?keira-phoebe

michellera7

4/19/2024 9:34:14 AM
20 anal sex toys that are perfect for beginners self https://police-stepbrother.fetish-matters.com/?desirae-keely

cathleennl16

4/18/2024 6:17:46 AM
Free best video converter without audio delay for my nokia 5800 https://tattooed-girl-amateur-anal.fetish-matters.com/?jaime-arely

bettyefz4

4/16/2024 7:12:24 PM
10 must read books for dating coaches for 2024 coach https://culazo-shemales.fetish-matters.com/?arlene-yvonne

kellierm60

3/18/2024 8:54:12 PM
Vatican approves blessings for same sex couples in landmark ruling http://hairy.porn-hornylynn69.hotnatalia.com/?mireya-meghan hether brooke porn vids sit on my foot porn disney porn videos clips top free porn sties porn hiv male actor

kenmw1

9/24/2022 3:03:20 AM
Hot teen pics http://werewolveporn.adablog69.com/?joanna gay porn s m jill 2 porn game sex in crazy places porn d3 somee porn galleries top ten porn movies new relaease

Daniel

9/23/2022 12:33:48 PM
Hi there, I am reaching out to see if you're interested in starting an ad campaign or improving the performance of an existing campaign on platforms like Google Ads, Bing Ads, Facebook Ads, and more by using PPC (Pay-Per-Click)? I am an expert in online advertising, and I will provide guaranteed traffic to your site, which can have a huge impact on your sales. Let me know if you're interested. Thanks, Daniel -- Daniel Todercan Owner, New Light Digital Our website: https://newlightdigital.com/services/ppc/ My phone number: 917-744-9170

Leave a Comment

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