Boost logo

Boost Users :

From: Dale Peakall (dale.peakall_at_[hidden])
Date: 2002-03-08 05:46:36


> 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.

// global variables
boost::mutex g_mutex;
boost::condition g_is_ready;

// threads that are waiting
{
        boost::mutex::scoped_lock lock(g_mutex);
        while(!m_x->isReady())
                g_is_ready.wait(lock);

        doSomething(); // do our stuff now that other thread is ready
}

// thread that we're waiting on
{
        ...

        g_is_ready.notify_one();
}

        - Dale.

P.S. Sorry for the incomplete message before, my laptop has interesting
key combos that keep catching me out.


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