1// excerpts from http://code.google.com/p/muduo/ 2// 3// Use of this source code is governed by a BSD-style license 4// that can be found in the License file. 5// 6// Author: Shuo Chen (giantchen at gmail dot com) 7 8#ifndef MUDUO_BASE_COUNTDOWNLATCH_H 9#define MUDUO_BASE_COUNTDOWNLATCH_H 10 11#include "Mutex.h" 12#include "Condition.h" 13 14#include <boost/noncopyable.hpp> 15 16namespace muduo 17{ 18 19class CountDownLatch : boost::noncopyable 20{ 21 public: 22 23 explicit CountDownLatch(int count) 24 : mutex_(), 25 condition_(mutex_), 26 count_(count) 27 { 28 } 29 30 void wait() 31 { 32 MutexLockGuard lock(mutex_); 33 while (count_ > 0) 34 { 35 condition_.wait(); 36 } 37 } 38 39 void countDown() 40 { 41 MutexLockGuard lock(mutex_); 42 --count_; 43 if (count_ == 0) 44 { 45 condition_.notifyAll(); 46 } 47 } 48 49 int getCount() const 50 { 51 MutexLockGuard lock(mutex_); 52 return count_; 53 } 54 55 private: 56 mutable MutexLock mutex_; 57 Condition condition_; 58 int count_; 59}; 60 61} 62#endif // MUDUO_BASE_COUNTDOWNLATCH_H 63