Learning from conditional variables in Ace

Source: Internet
Author: User

One of the methods for Ace thread synchronization is to use conditional variables. I didn't really understand them, mainly because Windows

 

There is no corresponding kernel object in the operating system, and the condition variable must be used together with mutex,

 

This is confusing. After repeated tests, we checked the source code of ACE,

 

I finally understand why conditional variables are always used together with mutex.

 

The sample code is as follows:

 

Int
Ha_commandhandler: SVC (void)
{
Ace_debug (lm_debug,
Ace_text ("(% t) handler thread running/N ")));

For (INT I = 0; I <num_uses; I ++)
{
This-> mutex _. Acquire ();
While (! This-> rep _. is_free ())
{
Ace_debug (lm_debug,
Ace_text ("(% t) thread begin wait/N ")));
This-> waitcond _. Wait ();
}
This-> rep _. set_owner (this );
This-> mutex _. Release ();

This-> rep _. update_device (I );

Ace_assert (this-> rep _. is_owner (this ));
This-> rep _. set_owner (0 );

This-> waitcond _. Signal ();
}

Return 0;
}

 

When multiple threads use conditional variables, during the wait operation, the function first performs this-> mutex _

 

Call the release operation, and then call waitforsingleobject to wait for a beacon (in Windows ),

 

When the beacon signal is released, waitforsingleobject is triggered, wait for the end, and then wait internally calls this-> mutex _

 

The acquire method of continues to be locked with mutex.

 

The advantage of this design is that when the thread suspends and waits, it does not occupy mutex resources, which can improve the concurrent execution efficiency of the thread.

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.