Boost logo

Boost :

From: Scott McCaskill (scott_at_[hidden])
Date: 2001-08-13 14:25:08


> > > OK, but why is this better than the event-based solution? IOW what is
the
> > > rationale for the added complexity?
> >
> > That's a different question.
>
> Well this is the question being discussed, isn't it? :-)

Fair enough :) I was just trying not to assume what you hadn't written..

> Not really; boost::waitFor() need not be constrained in the way the win32
> API is, if this functionality is important.
>

True.

> [...]
>
> > As I said, I don't have a lot of experience with events, but maybe
someone
> > who does can show me the most elegant way to do the equivalent of the
> > following using events:
> >
> > mutex m;
> > condition cv;
> > volatile bool a = false, b = false, c = false;
> >
> > void threadfunc()
> > {
> > mutex::scoped_lock lock( m );
> > while ( !( a || b || c ) )
> > cv.wait( lock );
> > if ( a )
> > do_something_a();
> > if ( b )
> > do_something_b();
> > if ( c )
> > do_something_c();
> > }
>
> Did you forget a loop? There's no point threading otherwise.
>

Ok, sure--I was just trying to simplify as much as possible.

> >
> > void notifyThread( bool _a, bool _b, bool _c )
> > {
> > { mutex::scoped_lock lock( m ); a = _a; b = _b; c = _c; }
> > cv.notify_one();
> > }
>
> synchronized_queue<int> q;
> event e;
>
> void threadfunc()
> {
> for(;;)
> {
> waitFor(e);
>
> while(!q.empty())
> {
> int m = q.pop();
> if(m & 1) do_something_a();
> if(m & 2) do_something_b();
> if(m & 4) do_something_c();
> }
> }
> }
>
> void notifyThread(bool a, bool b, bool c)
> {
> q.push(a + 2 * b + 4 * c);
> e.raise();
> }
>
> Did I utterly miss the point? :-)
>

Sort of. If there is only one thread executing threadFunc(), then this is
fine. But if you have more than one such worker thread then there is a race
condition between !q.empty() and q.pop(), no?


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