How to find other threads that send a signal when one thread calls WaitOne?

One of the things that I find difficult to understand in multi-threaded programming is the fact that when one thread reaches the line that calls WaitOne() , how do I know which other threads are involved? Where and how can I find (or understand) how WaitHandle receives a signal? For example, I'm looking at this code right now:

 private void RunSync(object state, ElapsedEventArgs elapsedEventArgs) { _mutex.WaitOne(); using (var sync = GWSSync.BuildSynchronizer(_log)) { try { sync.Syncronize(); } catch(Exception ex) { _log.Write(string.Format("Error during synchronization : {0}", ex)); } } _mutex.ReleaseMutex(); _syncTimer.Interval = TimeBeforeNextSync().TotalMilliseconds; _syncTimer.Start(); } 

There are several such methods in the file (for example, RunThis (), RunThat ()). These methods run inside the Windows service and are called when the timer expires. Each of these methods is called using different timers and is configured as follows:

  //Synchro var timeBeforeFirstSync = TimeBeforeNextSync(); _syncTimer = new System.Timers.Timer(timeBeforeFirstSync.TotalMilliseconds); _syncTimer.AutoReset = false; _syncTimer.Elapsed += RunSync; _syncTimer.Start(); 

I understand that when the timer expires, the RunSync method will be launched. But when it hits the WaitOne() , the thread is blocked. But who is waiting for this? What "other" stream will send the signal?

+5
source share
1 answer

WaitHandle is an abstraction, as stated in the documentation:

Encapsulates objects that depend on the operating system that are waiting for exclusive access to shared resources.

You don't know what other threads are involved, but you do know what other code is involved by checking usage ( _mutex in your case). Each derived WaitHandle class inherits WaitOne , but what happens after a successful wait and how it is signaled is specific. For example, in your example _mutex , most likely there is a Mutex class, so WaitOne acts like "wait until it becomes free and owns," while ReleaseMutex acts as a "release of ownership and signal." With that in mind, it should be obvious what all of these methods do - ensuring that while RunThis you can't RunThat and vice versa.

+1
source

All Articles