race condition in WaitGroup class
I think that the implementation is racy, especially the Add/Done side. According to this link https://www.modernescpp.com/index.php/c-core-guidelines-be-aware-of-the-traps-of-condition-variables atomic variable modification should be made under the lock
I think that the implementation is racy, especially the Add/Done side. According to this link https://www.modernescpp.com/index.php/c-core-guidelines-be-aware-of-the-traps-of-condition-variables atomic variable modification should be made under the lock
Agreed on this.
// this would be called from the main thread
void WaitGroup::Add(int i) {
std::unique_lock<std::mutex> l(mu_);
counter_ += i;
}
// this would be called from the worker thread
void WaitGroup::Done() {
{
std::unique_lock<std::mutex> l(mu_);
counter_--;
}
cv_.notify_all();
}
// this would be called from the main thread
void WaitGroup::Wait() {
std::unique_lock<std::mutex> l(mu_);
cv_.wait(l, [&] { return counter_ <= 0; });
}
@qiandu2006 @yoda-jm thank you very much for your issue. I will update it once I have again a c++ development environment setup. Feel free to create a PR. I appreciate your review, thank you!
I leave it open until I have time to fix it or someone has sent a PR.