CountDownLatch.cs 616 B

123456789101112131415161718192021222324252627282930
  1. using System.Threading;
  2. namespace Top.Api.Util
  3. {
  4. public class CountDownLatch
  5. {
  6. private int count;
  7. private EventWaitHandle ewh;
  8. public CountDownLatch(int count)
  9. {
  10. this.count = count;
  11. this.ewh = new ManualResetEvent(false);
  12. }
  13. public void Signal()
  14. {
  15. // The last thread to signal also sets the event.
  16. if (Interlocked.Decrement(ref count) == 0)
  17. {
  18. this.ewh.Set();
  19. }
  20. }
  21. public void Wait()
  22. {
  23. this.ewh.WaitOne();
  24. }
  25. }
  26. }