|
Boost : |
From: Scott McCaskill (scott_at_[hidden])
Date: 2001-08-13 09:24:31
> This assumption is dangerous. I would say that "most people" simply either
> don't understand the issues involved, or don't care.
>
> I'm not afraid to admit that I fall in the first category.
>
> For instance, what is the boost.threads way to do the following:
>
> void thread_function()
> {
> init();
>
> for(;;)
> {
> waitForMultipleEvents(wakeup_event, terminate_event);
> if(terminate_event signalled) break;
> do_work();
> }
>
> cleanup();
> }
>
How about this:
boost::mutex theMutex;
boost::condition theCondition;
volatile bool terminateFlag = false;
volatile bool wakeupFlag = false;
void thread_function()
{
init();
for(;;)
{
{
boost::mutex::scoped_lock lock( theMutex );
while ( !terminateFlag && !wakeupFlag )
{
// (release the mutex and wait for notification;
// when this returns, we have the mutex again)
theCondition.wait( lock );
}
if ( terminateFlag )
break;
if ( wakeupFlag )
wakeupFlag = false;
}
do_work();
}
cleanup();
}
void tellThreadToStop()
{
{
boost::mutex::scoped_lock lock( theMutex );
terminateFlag = true;
}
theCondition.notify_one();
}
void tellThreadToWakeUp()
{
{
boost::mutex::scoped_lock lock( theMutex );
wakeupFlag = true;
}
theCondition.notify_one();
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk