27 ThreadBlockerUntilZero
No longer used so the code is only recorded here in this web-page.
Provides a signed threadsafe counter that can be incremented and decremented and take on positive or negative values, and allows for waiting until the counter reaches zero.
class ThreadBlockerUntilZero
{
cxNotCloneable(ThreadBlockerUntilZero)
public:
explicit ThreadBlockerUntilZero(int count = 0) : count_(count) {}
~ThreadBlockerUntilZero()
{
cxAssert(count_ == 0);
}
int operator++()
{
std::lock_guard<std::mutex> lock(mutex_);
if (++count_ == 0)
{
cv_.notify_all();
}
return count_;
}
int operator--()
{
std::lock_guard<std::mutex> lock(mutex_);
if (--count_ == 0)
{
cv_.notify_all();
}
return count_;
}
// Postfix
int operator++(int) { return operator++()-1; }
int operator--(int) { return operator--()+1; }
void Wait()
{
std::unique_lock<std::mutex> lock(mutex_);
cv_.wait(lock,[this]{return count_ == 0;});
}
private:
std::mutex mutex_;
std::condition_variable cv_;
int count_;
};