Boost logo

Boost :

From: William Kempf (sirwillard_at_[hidden])
Date: 2000-08-10 14:39:43


--- In boost_at_[hidden], jsiek_at_l... wrote:
>
> So one thing that bothers me about pthreads is the complexity of the
> usage scenario for condition variables. Have any of you seen ways to
> simplify this?
>
> Just as a reminder, here's how the usage goes:
>
> Thread 1:
>
> pthread_mutex_lock(&m);
> while (!some_condition)
> pthread_cond_wait(&cv, &m);
> // now some_condition == true
> // do something useful
> pthread_mutex_unlock(&m);
>
>
> Thread 2:
>
> pthread_mutex_lock(&m);
> some_condition = true;
> pthread_mutex_unlock(&m);
> pthread_cond_signal(&cv);
>
>
> Maybe there's a way to hide that while loop if a Predicate function
> object is attached to the condition variable?

What really bothers me is the need to obtain the lock before calling
the wait but with out compile time requirements for this. I'd really
like a compile time requirement in our design (if we have mutexes and
conditions... it sounds like this will be debated).

As for the predicate to remove the while... I considered that a lot
yesterday while thinking on this subject. The only problem I see is
that you don't always have a predicate or while loop. Sometimes the
signal is enough. Other than that, the idea appeals to me. It makes
the code a little easier to read and understand (especially using
lambda library), though it's not really any easier to write. If we
always require the predicate it would also insure that someone new to
the concept is more likely to use the condition correctly, though it
would require a "dummy predicate" in the corner cases where a
predicate is not needed.

I wouldn't attach it to the condition, however. I'd pass the
predicate into the wait. Thus the condition can be used to track
different criteria.

   cond.wait((free1 > 10)(a));

William Kempf


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