12: two-way signals and Competitions

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.