Synchronize the execution of multiple threads using a semaphore.

Problem: You need to control the number of threds that can access a shared resource or section of code concurerently.

Solutuin: You can use the System.Threading.Semaphore class.

The Code: The following example demonstrates how to use a named semaphore to limit access to shared resource to two threads at any given time.

——————

using System;
using System.Threading;

namespace Synchronize_Semaphoere
{
    class SSemaphore
    {
        static bool terminate = false;

        private static void TraceMsg(string msg)
        {
            Console.WriteLine("[{0,3}] – {1} : {2}",
                Thread.CurrentThread.ManagedThreadId,
                DateTime.Now.ToString("HH:mm:ss.ffff"), msg);
        }

        private static void DisplayMessage()
        {
            using (Semaphore sem = Semaphore.OpenExisting("SemaphoreExample"))
            {
                TraceMsg("Thread started.");

                while (!terminate)
                {
                    sem.WaitOne();
                    TraceMsg("Thread owns the Semaphore.");
                    Thread.Sleep(1000);
                    TraceMsg("Thread releasing the Semaphore.");
                    sem.Release();
                    Thread.Sleep(100);
                }
                TraceMsg("Thread terminating.");
            }
        }

        public static void Main()
        {
            using (Semaphore sem = new Semaphore(2, 2, "SemaphoreExample"))
            {
                TraceMsg("Starting threads — press Enter to terminate.");
                Thread trd1 = new Thread(DisplayMessage);
                Thread trd2 = new Thread(DisplayMessage);
                Thread trd3 = new Thread(DisplayMessage);
                trd1.Start();
                trd2.Start();
                trd3.Start();

                Console.ReadLine();

                terminate = true;
                trd1.Join(5000);
                trd2.Join(5000);
                trd3.Join(5000);
            }
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Main method complete. Press Enter.");
            Console.ReadLine();
        }
    }
}

 

 

using System;
using System.Threading;

namespace Synchronize_Semaphoere
{
    class SSemaphore
    {
        static bool terminate = false;

        private static void TraceMsg(string msg)
        {
            Console.WriteLine("[{0,3}] – {1} : {2}",
                Thread.CurrentThread.ManagedThreadId,
                DateTime.Now.ToString("HH:mm:ss.ffff"), msg);
        }

        private static void DisplayMessage()
        {
            using (Semaphore sem = Semaphore.OpenExisting("SemaphoreExample"))
            {
                TraceMsg("Thread started.");

                while (!terminate)
                {
                    sem.WaitOne();
                    TraceMsg("Thread owns the Semaphore.");
                    Thread.Sleep(1000);
                    TraceMsg("Thread releasing the Semaphore.");
                    sem.Release();
                    Thread.Sleep(100);
                }
                TraceMsg("Thread terminating.");
            }
        }

        public static void Main()
        {
            using (Semaphore sem = new Semaphore(2, 2, "SemaphoreExample"))
            {
                TraceMsg("Starting threads — press Enter to terminate.");
                Thread trd1 = new Thread(DisplayMessage);
                Thread trd2 = new Thread(DisplayMessage);
                Thread trd3 = new Thread(DisplayMessage);
                trd1.Start();
                trd2.Start();
                trd3.Start();

                Console.ReadLine();

                terminate = true;
                trd1.Join(5000);
                trd2.Join(5000);
                trd3.Join(5000);
            }
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Main method complete. Press Enter.");
            Console.ReadLine();
        }
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s