Thread Synchronization with ManualResetEventSlim Construct

What is ManualResetEventSlim and how to synchronize threads using ManualResetEventSlim


Category: Multithreading Tags: C#


Introduction

        As we seen in our last article about AutoResetEvent how it blocks and unblocks a thread using signaled and non-signaled states. AutoResetEvent had a limitation like it will let only one thread pass at a time in signaled state and once thread is unblocked it again goes in non-signaled state automatically unlike ManualResetEventSlim which doesn't go in non-signaled state automatically once signaled and need to call Reset method manually to send it in non-signaled state.

Implementation

        See code below:

class Program
{
    static ManualResetEventSlim eventManualWorker = new ManualResetEventSlim(false);
    static void Main(string[] args)
    {
        Thread t1 = new Thread(() => ManualResetFun(3000));
        Thread t2 = new Thread(() => ManualResetFun(5000));
        Thread t3 = new Thread(() => ManualResetFun(8000));

        t1.Name = "thread1";
        t2.Name = "thread2";
        t3.Name = "thread3";

        t1.Start();
        t2.Start();
        t3.Start();

        eventManualWorker.Set();
        Console.WriteLine("Main thread stopped for 6000 miliseconds");
        Thread.Sleep(6000);
        eventManualWorker.Reset();
        Console.WriteLine("Main thread stopped for 4000 miliseconds");
        Thread.Sleep(4000);
        eventManualWorker.Set();
        Console.ReadLine();
    }

    static void ManualResetFun(int seconds)
    {
        Console.WriteLine("Printing started by {0}", Thread.CurrentThread.Name);
        Console.WriteLine("{0} stopped for {1} miliseconds", Thread.CurrentThread.Name, seconds);
        Thread.Sleep(seconds);
        eventManualWorker.Wait();
        Console.WriteLine("Printing completed by {0}", Thread.CurrentThread.Name);
    }
}

Above we can see we are running three threads which are printing some lines and having different sleep times. Once we start threads we call set method of ManulResetEventSlim that changes it's state from non-signaled to signaled then Main thread goes in sleep for 6 seconds, threads start printing and goes in sleep for 3, 5 and 8 seconds, by that time ManualResetEventSlim is already in signaled state so thread t1 and t2 complete their execution. After 6 seconds main thread continues execution and calls Reset method and ManualResetEventSlim goes back to non-signaled state now thread 3 which was sleeping for 8 seconds can not complete execution, after 4 seconds main thread calls Set method again and t3 completes execution. The output of this code will be:

Main thread stopped for 6000 miliseconds
Printing started by thread1
thread1 stopped for 3000 miliseconds
Printing started by thread2
thread2 stopped for 5000 miliseconds
Printing started by thread3
thread3 stopped for 8000 miliseconds
Printing completed by thread1
Printing completed by thread2
Main thread stopped for 4000 miliseconds
Printing completed by thread3


Like 0 People
Last modified on 11 October 2018
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 126
Questions: 9
Given Best Solutions: 8 *

Comments:

No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x