Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Thread synchronization & signaling

$
0
0

what is thread synchronization and what thread signaling. if possible explain in easy way with example.

here i am pasting code.

class SimpleWaitPulse
{
  static readonly object _locker = new object();
  static bool _go;
  static void Main()
  {                                // The new thread will block
    new Thread (Work).Start();     // because _go==false.
    Console.ReadLine();            // Wait for user to hit Enter
    lock (_locker)                 // Let's now wake up the thread by
    {                              // setting _go=true and pulsing.
      _go = true;
      Monitor.Pulse (_locker);
    }
  }
  static void Work()
  {
    lock (_locker)
      while (!_go)
        Monitor.Wait (_locker);    // Lock is released while we’re waiting
 
    Console.WriteLine ("Woken!!!");
  }
}

what Monitor.Pulse() & Monitor.Wait() does in the program ?

help me to understand the flow of the above program that how it works?

i have seen people use many classes for thread synchronization like

Monitor.Pulse() & Monitor.Wait()
AutoResetEvent 
ManualResetEvent 
Semaphore

so i like to know all the above technique works for the same situation....i think no. if all the classes does the same job then why MS wrote same kind of classes having different name but same in functionality.

if possible briefly tell me when to use Semaphore or when to useAutoResetEvent or ManualResetEvent.

Monitor.Pulse() & Monitor.Wait() other lots more exist.

thanks


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>