muduo C++11 重写CountDownLatch.h
时间: 2024-01-20 21:16:28 浏览: 95
C++指针汇总
```cpp
#ifndef MUDUO_BASE_COUNTDOWNLATCH_H
#define MUDUO_BASE_COUNTDOWNLATCH_H
#include "Condition.h"
#include "Mutex.h"
namespace muduo
{
class CountDownLatch : noncopyable
{
public:
explicit CountDownLatch(int count)
: mutex_(),
condition_(mutex_),
count_(count)
{
}
void wait()
{
MutexLockGuard lock(mutex_);
while (count_ > 0)
{
condition_.wait();
}
}
void countDown()
{
MutexLockGuard lock(mutex_);
--count_;
if (count_ == 0)
{
condition_.notifyAll();
}
}
int getCount() const
{
MutexLockGuard lock(mutex_);
return count_;
}
private:
mutable MutexLock mutex_;
Condition condition_;
int count_;
};
} // namespace muduo
#endif // MUDUO_BASE_COUNTDOWNLATCH_H
```
阅读全文