|
Boost Users : |
From: Colin Fox (cfox_at_[hidden])
Date: 2002-03-08 03:36:05
On Thu, 2002-03-07 at 17:01, simonwlb wrote:
> I'm using thread::yield() in my multi-threaded cross-platform
> application to avoid "CPU burn".
<snip>
> {
> if( m_x->isReady() )
> doSomething();
<..>
>
> If the answer is "you should almost never poll like that - try to use
> mutexes etc. as much as possible" please say so (I'm a newbie to
> threads)!
The answer is actually probably conditionals plus mutexes. Presumably
one thread is doing something, and other threads are waiting for it to
be ready. This is a standard producer/consumer model.
The consumers all wait on a condition variable, and the producer grabs
the mutex, signals the condition variable, and then releases the mutex.
One (or all, depending on how you signal) of the threads will wake up to
deal with the signal. The advantage of this is the threads waiting on
the signal are truly asleep, using 0% cpu.
If you were using pthreads, I'd suggest looking up pthread_cond_signal
and it's bretheren. I unfortunately don't know Boost's equivalents, but
I'm sure they're similar.
-- Colin Fox cfox_at_[hidden] CF Consulting Inc. GPG Fingerprint: D8F0 84E7 E7CC 5C6C 9982 F1A7 A3EB 6EA3 BC97 572F [Non-text portions of this message have been removed]
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net