
On Sep 23, 2005, at 6:37 PM, Jason Hise wrote:
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?
Ideally you would use a system by which the all threads which can't proceed sleep and all threads which can proceed don't sleep. If the main thread can't exit because of an ongoing worker thread, then it sleeps, and one would hope that if no other threads are contending, this would force the worker thread awake to complete its task. If join isn't behaving as above, there is a bug, perhaps in boost code, perhaps in the OS below boost code. Or are we talking about a worker thread that has been forced to sleep for X amount of time? I.e. part of its task is to sleep. If so, why is that worker thread forced to sleep, and perhaps having it sleep on a condition variable would be better? -Howard