Boost logo

Boost :

From: Anthony Williams (anthony_w.geo_at_[hidden])
Date: 2005-09-23 17:44:46


Jason Hise <chaos_at_[hidden]> writes:

> Peter Dimov wrote:
>
>>Jason Hise wrote:
>>
>>
>>>Can the yield function of boost::thread be used to immediately wake
>>>up a thread that is sleeping? I would like to use this to make sure
>>>that certain threads get the chance to exit before the process terminates,
>>>without join resulting in the whole program sleeping.
>>>
>>>
>>
>>Join only blocks the current thread, not the whole program. If you want
>>thread X to exit before the process is terminated (via a return from main,
>>for instance), then joining X before returning from main or calling exit()
>>would be the appropriate way to accomplish that.
>>
> The problem is that joining x before main exits forces the process to
> wait until x is done sleeping before main can exit. If there is no way
> to wake up a sleeping thread, then it appears my only option is to sleep
> for very short intervals and keep checking two conditions to see if
> sleeping should stop (time elapsed, or early wakeup signaled).
> Unfortunately, that will be expensive not only because the thread can't
> sleep 'soundly', but also because each time I want to check if the wake
> up signal has been triggered I need to acquire a lock. Plus, while I am
> waiting for that lock the time could elapse and I would still be
> waiting. Is there any other way to pull this off?

This sounds like a prime target for the use of a condition variable to me. You
can wait on a condition variable, then signal the condition variable to wake
the sleeping thread. Yes it means acquiring a lock, but most thread
synchronization requires some form of lock.

Anthony

-- 
Anthony Williams
Software Developer
Just Software Solutions Ltd
http://www.justsoftwaresolutions.co.uk

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk