count_down_latch.h 528 B

123456789101112131415161718192021222324252627282930
  1. #pragma once
  2. #include <library/cpp/deprecated/atomic/atomic.h>
  3. #include <util/system/event.h>
  4. class TCountDownLatch {
  5. private:
  6. TAtomic Current;
  7. TSystemEvent EventObject;
  8. public:
  9. TCountDownLatch(unsigned initial)
  10. : Current(initial)
  11. {
  12. }
  13. void CountDown() {
  14. if (AtomicDecrement(Current) == 0) {
  15. EventObject.Signal();
  16. }
  17. }
  18. void Await() {
  19. EventObject.Wait();
  20. }
  21. bool Await(TDuration timeout) {
  22. return EventObject.WaitT(timeout);
  23. }
  24. };