Boost logo

Boost :

Subject: [boost] [threads] API Design Question
From: Alexander Bernauer (alex_at_[hidden])
Date: 2009-02-04 09:28:50


Why is it, that the wait and timed_wait functions of the
condition_variable class do not encapsulate the problem of spurious

I wonder whether there are good reasons. I have seen libraries which do
so. Do they miss a corner case?

For illustration here is a sketch of a wrapper class
class save_condition_variable {
   save_condition_variable() : waiting(0), wakeup(0) { }

   void notify_one()
      if (wakeup < waiting) {

   void notify_all()
      wakeup = waiting;

   void wait(unique_lock<mutex>& lock)
      do { cond.wait(lock) } while (wakeup==0);

   bool timed_wait(unique_lock<mutex>& lock, system_time const& abs_time)
      bool res = true;
      while(wakeup==0 and res) {
         res = cond.timed_wait(lock, abs_time);
      if (res) --wakeup;
      return res;

   condition_variable cond;
   int waiting;
   int wakeup;

Having the spurious wakeup problem handled by the library is a
significant increase of comfort for both developers and maintainers.



Boost list run by bdawes at, gregod at, cpdaniel at, john at