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.