lgqhaoyue / google-concurrency-library

Automatically exported from code.google.com/p/google-concurrency-library
0 stars 0 forks source link

Implementation of countdown_latch::wait() #11

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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