Two-way signaling and races)
Monitor. PulseAn important feature of a method is that it is executed asynchronously, which means thatPulseThe method does not block your waitingMonitor. Pulse. If any threadPulsedWait on the object, it will not block, in other words, callMonitor. PulsePairProgramYou can think thatMonitor. PulseMethod is ignored.
This wayPulseProvides one-way communication: OnePulsingThe thread quietly sendsWaitingThe thread sends a signal.
PulseIt does not return a value to tell youWaitingWhether the thread receives the signal.
But sometimes we need to knowWaitingIndicates whether the thread receives signals. For example, the following example shows:
Class Race
{
Static Readonly Object _ Locker = New Object ();
Static Bool _ Go;
Public Static VoidMainthread ()
{
NewThread (saysomething). Start ();
For ( Int I = 0 ; I < 5 ; I ++ )
{
Lock (_ Locker)
{
_ Go = True ;
Monitor. pulseall (_ locker ); // Queue waiting for notification
}
}
}
Static Void Saysomething ()
{
For ( Int I = 0 ; I < 5 ; I ++ )
{
Lock (_ Locker)
{
While ( ! _ Go) monitor. Wait (_ locker ); // If _ Go is false, blocking starts.
_ Go = False ;
Console. writeline ( " Wassup? " );
}
}
}
}
Expected output:
Wassup?
Wassup?
Wassup?
Wassup?
Wassup?
Actual output:
Wassup? (Thread wait)
InSaysomethingMethod,ForLoopWhile._ GoIsFalse, SoMonitor. WaitStart waiting. InMainthreadMedium,ForLoop settings_ GoIsTrue. ThenPulseall.HoweverPulseallThe method is asynchronous.
ThereforeSaysomethingBefore the thread is awakened,MainthreadInForThe loop may have been executed. SoSaysomethingThe first one in the MethodWaitThe message received by the thread is_ GoIsTrueSo execute the following command again_ GoSet the fieldFalse. Output"Wassup?",However, the next cycle_ GoIsFalse, So you needWait.Therefore, the actual output printsWassupAnd then start waiting.
We need the main threadWorkerThe main thread is blocked when the previous task is still being executed. WaitWorkerAfter the execution is completed, the main thread resumes the execution and then executes the iteration.
We can add_ ReadyTo control the main thread in the settings_ GoBefore flagWorkerThe thread hasReady. That is to say, the main thread is setting_ GoBefore, will waitWorkerComplete the task and waitWorkerSetReadySetTrue, WhenWorkerSetReadySetTrueAfterPulseTo notify the main thread.
Class Race
{
Static Readonly Object _ Locker = New Object ();
Static Bool _ Ready, _ go;
Public Static VoidMainthread ()
{
NewThread (saysomething). Start ();
For ( Int I = 0 ; I < 5 ; I ++ )
{
Lock (_ Locker)
{
While ( ! _ Ready) monitor. Wait (_ locker ); // If ready of worker is false, wait for worker.
_ Ready = False ; // Reset flag
_ Go = True ;
Monitor. pulseall (_ locker );
}
}
}
Static Void Saysomething ()
{
For ( Int I = 0 ; I < 5 ; I ++ )
{
Lock (_ Locker)
{
_ Ready = True ; // Set ready to true
Monitor. pulseall (_ locker ); // Notify the main thread that the worker is ready to run.
While ( ! _ Go) monitor. Wait (_ locker );
_ Go = False ;
Console. writeline ( " Wassup? " );
}
}
}
}
References:
Http://www.albahari.com/threading/
CLR via C #3.0