CountDownLatch
1 构造函数的实现
CountDownLatch::CountDownLatch(int count)
: mutex_(),
condition_(mutex_),
count_(count)
{
}
2 普通成员函数的实现
void wait()函数的实现
这个函数主要是当条件不满足的时候,就阻塞等待条件满足
void CountDownLatch::wait()
{
//加锁
MutexLockGuard lock(mutex_);
while (count_ > 0)
{
//如果条件满足,就一直阻塞等待
condition_.wait();
}
}
void countDown(),该函数主要实现对计数器的–操作
void CountDownLatch::countDown()
{
MutexLockGuard lock(mutex_);
--count_;
//当计数器为0的时候,条件变量就通知所有线程
if (count_ == 0)
{
condition_.notifyAll();
}
}
int CountDownLatch::getCount()函数的实现
int CountDownLatch::getCount() const
{
//获取计数器的时候,首先进行加锁
MutexLockGuard lock(mutex_);
return count_;
}