In the previous article we discussed about locking options in c# , in this article we will see the signaling methods , Signaling is a concept where is you notify a thread to continue its execution which is initially waiting for some other thread to finish executing a section of code .
AutoResetEvent,ManulResetEvent,CountDownEvent,Wait and Pulse,Barrier are the options available for signaling threads in C#.
AutoResetEvent
AutoResetEvent is analogous to ticket turnstile or toll booth where in only one ticket lets one vehicle through and the toll gate is automatically closed .
a thread is made to wait with the WaitOne keyword and a call to set lets the waiting thread to continue. below is the trivial example on how to use AutoResetEvent To signal .
class ThreadSignaling
{
static AutoResetEvent autoResetEvent = new AutoResetEvent(false);
static void Main(string[] args)
{ new Thread(WaitingThread).Start();
Console.WriteLine("Main Theard :simulating long running task");
Thread.Sleep(3000);
Console.WriteLine("Main Theard :sending signal using AutoResetEvent");
autoResetEvent.Set();
}
private static void WaitingThread()
{
Console.WriteLine("WaitingThread:Enters in waiting mode -- ");
autoResetEvent.WaitOne();
Console.WriteLine("WaitingThread: the signal from a diffrent thread with AutoResetEvent");
Console.ReadKey();
}
}
ManulResetEvent:
ManualResetEvent works more like a school gate where is once the gate us open all the students waiting are allowed to go through the gate , Calling waitOne will block the thread and once Set is called all the threads that were block with WaitOne continues its execution at a time unlike AutoResetEvent Where one call to Set releases only one WaitOne Counter part ..
below is the sample code which demonstrates the same .
below is the sample code which demonstrates the same .
class ThreadSignaling
{
static ManualResetEvent autoResetEvent = new ManualResetEvent(false);
static void Main(string[] args)
{
new Thread(WaitingThread1).Start();
new Thread(WaitingThread1).Start();
Console.WriteLine("Main Theard :simulating long running task");
Thread.Sleep(3000);
Console.WriteLine("Main Theard :sending signal using ManualResetEvent");
autoResetEvent.Set();
}
private static void WaitingThread1()
{
Console.WriteLine("WaitingThread1:Enters in waiting mode -- ");
autoResetEvent.WaitOne();
Console.WriteLine("WaitingThread1: the signal from a diffrent thread with ManualResetEvent");
Console.ReadKey();
}
private static void WaitingThread2()
{
Console.WriteLine("WaitingThread2:Enters in waiting mode -- ");
autoResetEvent.WaitOne();
Console.WriteLine("WaitingThread2: the signal from a diffrent thread with ManualResetEvent");
Console.ReadKey();
}
}
CountDownEvent(.NET 4.0 and above)
CountDownEvent allows a thread to wait till you get a configured number of signals from different threads before the thread continues . The thread gets blocked by calling Wait and the signal is passed to the waiting construct by calling Signal .
below is a trivial example of CountDownEvent
class ThreadSignaling
{
static CountdownEvent countDownEvent = new CountdownEvent(2);
static void Main(string[] args)
{
new Thread(WaitingThread).Start();
new Thread(SignalThread1).Start();
new Thread(SignalThread2).Start();
Console.ReadKey();
}
private static void WaitingThread()
{
Console.WriteLine("WaitingThread1:Enters in waiting mode -- ");
countDownEvent.Wait();
Console.WriteLine("WaitingThread1: Continued after reciving two signals from diffrent threads");
Console.ReadKey();
}
private static void SignalThread1()
{
Console.WriteLine("SignalThread1:Simulating long running task ");
Thread.Sleep(3000);
countDownEvent.Signal();
Console.WriteLine("SignalThread1: Sends the signal as the task is done ");
Console.ReadKey();
}
private static void SignalThread2()
{
Console.WriteLine("SignalThread2:Simulating long running task ");
Thread.Sleep(5000);
countDownEvent.Signal();
Console.WriteLine("SignalThread2: Sends the signal as the task is done ");
Console.ReadKey();
}
}
Above we looked into simpler version of signaling, in the next article we will discuss about more powerful construct to achieve signaling for threads using Wait and Pulse from the Monitor Class and also discuss about Barrier which is available for .net framework 4.0 and above .

0 comments:
Post a Comment