CountBarrier.cc 569 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "Thread/CountBarrier.h"
  2. namespace Egametang {
  3. CountBarrier::CountBarrier(int count):
  4. count(count), mutex(), condition()
  5. {
  6. }
  7. void CountBarrier::Wait()
  8. {
  9. boost::mutex::scoped_lock lock(mutex);
  10. while (count > 0)
  11. {
  12. condition.wait(lock);
  13. }
  14. }
  15. void CountBarrier::Signal()
  16. {
  17. boost::mutex::scoped_lock lock(mutex);
  18. --count;
  19. if (count == 0)
  20. {
  21. condition.notify_all();
  22. }
  23. }
  24. int CountBarrier::Count() const
  25. {
  26. boost::mutex::scoped_lock lock(mutex);
  27. return count;
  28. }
  29. void CountBarrier::Reset(int count)
  30. {
  31. this->count = count;
  32. }
  33. } // namespace Egametang