GrgrLmml/google-concurrency-library

Implementation of countdown_latch::wait()

Opened this issue · 1 comments

In the current implementation of countdown_latch::wait()
in src/countdown_latch.cc

void countdown_latch::wait() {
  unique_lock<mutex> lock(condition_mutex_);
  while(count_ > 0) {
    condition_.wait(lock);
  }
}

I think the while loop is redundant as the thread
has already blocked when count_ > 0 due to the statement

condition_.wait(lock);

I suggest to replace while by if as follows

void countdown_latch::wait() {
  unique_lock<mutex> lock(condition_mutex_);
  if(count_ > 0) {
    condition_.wait(lock);
  }
}

Original issue reported on code.google.com by letonch...@gmail.com on 8 Jan 2015 at 6:36

The current code looks correct to me.
Conditions variables can have spurious wakeups, so any usage of wait w/o a 
surrounding predicate-checking loop is a bug.
#Invalid

Original comment by dvyu...@google.com on 12 Jan 2015 at 8:04