ManualResetEvent Vs AutoResetEvent
ManualResetEvent can be used for notifying one or more threads that an event has occured. And unless and untill reset is called manually, reset of the signal is not done. Whereas AutoResetEvent can be used for notifying only one thread that the event has occured and again it resets its state.
Example:
class Program
{
public static int x = 0;
public static int y = 0;
public static int z = 0;
public static bool thread1Turn = true;
public static bool thread2Turn = true;
public static bool thread3Turn = true;
public static ManualResetEvent manualResetEvent = new ManualResetEvent(false);
public static AutoResetEvent autoResetEvent = new AutoResetEvent(false);
static void Main(string[] args)
{
Thread th1 = new Thread(fn1);
th1.Start();
Thread th3 = new Thread(fn3);
th3.Start();
Thread th2 = new Thread(fn2);
th2.Start();
Console.ReadLine();
}
static void fn1()
{
while(thread1Turn)
{
autoResetEvent.WaitOne();
//Now this thread is waiting for the event to go into signalled state.
Console.WriteLine("th1 value {0}",x );
x++;
if (x > 10)
break;
}
}
static void fn2()
{
while (thread2Turn)
{
Console.WriteLine("th2 value {0}",y );
autoResetEvent.Set();
Thread.Sleep(20);
y++;
if (y > 10)
break;
}
}
static void fn3()
{
while (thread3Turn)
{
autoResetEvent.WaitOne();
//Now this thread is waiting for the event to go into signalled state.
Console.WriteLine("th3 value {0}", z);
z++;
if (z > 10)
break;
}
}
}
OutPut: Output will be => No of th2 Values = th1 Values + th3 Values.
Here we cannot get all the 3 values(th1,th2,th3) in equal number. The same thing when we use only 2 threads(just remove th3 and related code) , we dont find much difference b/n Manual and AutoResetEvent we will find th1 and th2 in equal number.
Whereas now if your functionality needs such a way that th1 and th3 threads, both need to execute as soon as we get a signal from th2. Then it is better to use ManualResetEvent.
Example:
class Program
{
public static int x = 0;
public static int y = 0;
public static int z = 0;
public static bool thread1Turn = true;
public static bool thread2Turn = true;
public static bool thread3Turn = true;
public static ManualResetEvent manualResetEvent = new ManualResetEvent(false);
public static AutoResetEvent autoResetEvent = new AutoResetEvent(false);
static void Main(string[] args)
{
Thread th1 = new Thread(fn1);
th1.Start();
Thread th3 = new Thread(fn3);
th3.Start();
Thread th2 = new Thread(fn2);
th2.Start();
Console.ReadLine();
}
static void fn1()
{
while(thread1Turn)
{
autoResetEvent.WaitOne();
//Now this thread is waiting for the event to go into signalled state.
Console.WriteLine("th1 value {0}",x );
x++;
if (x > 10)
break;
}
}
static void fn2()
{
while (thread2Turn)
{
Console.WriteLine("th2 value {0}",y );
autoResetEvent.Set();
Thread.Sleep(20);
y++;
if (y > 10)
break;
}
}
static void fn3()
{
while (thread3Turn)
{
autoResetEvent.WaitOne();
//Now this thread is waiting for the event to go into signalled state.
Console.WriteLine("th3 value {0}", z);
z++;
if (z > 10)
break;
}
}
}
OutPut: Output will be => No of th2 Values = th1 Values + th3 Values.
Here we cannot get all the 3 values(th1,th2,th3) in equal number. The same thing when we use only 2 threads(just remove th3 and related code) , we dont find much difference b/n Manual and AutoResetEvent we will find th1 and th2 in equal number.
Whereas now if your functionality needs such a way that th1 and th3 threads, both need to execute as soon as we get a signal from th2. Then it is better to use ManualResetEvent.
Thanks for sharing.
ReplyDeleteI suggest you the usage of a code syntax highlighter to enhance the readibility: Pastebin, SyntaxHighlighter, or GitHub Gists.
Regards from Bogotá.