wait_group.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #pragma once
  2. #include <library/cpp/threading/future/core/future.h>
  3. #include <util/generic/ptr.h>
  4. namespace NThreading {
  5. namespace NWaitGroup::NImpl {
  6. template <class WaitPolicy>
  7. struct TState;
  8. template <class WaitPolicy>
  9. using TStateRef = TIntrusivePtr<TState<WaitPolicy>>;
  10. }
  11. // a helper class which allows to
  12. // wait for a set of futures which is
  13. // not known beforehand. Might be useful, e.g., for graceful shutdown:
  14. // while (!Stop()) {
  15. // wg.Add(
  16. // DoAsyncWork());
  17. // }
  18. // std::move(wg).Finish()
  19. // .GetValueSync();
  20. //
  21. //
  22. // the folowing are equivalent:
  23. // {
  24. // return WaitAll(futures);
  25. // }
  26. // {
  27. // TWaitGroup<TWaitPolicy::TAll> wg;
  28. // for (auto&& f: futures) { wg.Add(f); }
  29. // return std::move(wg).Finish();
  30. // }
  31. template <class WaitPolicy>
  32. class TWaitGroup {
  33. public:
  34. TWaitGroup();
  35. // thread-safe, exception-safe
  36. //
  37. // adds the future to the set of futures to wait for
  38. //
  39. // if an exception is thrown during a call to ::Discover, the call has no effect
  40. //
  41. // accepts non-void T just for optimization
  42. // (so that the caller does not have to use future.IgnoreResult())
  43. template <class T>
  44. TWaitGroup& Add(const TFuture<T>& future);
  45. // finishes building phase
  46. // and returns the future that combines the futures
  47. // in the wait group according to WaitPolicy
  48. [[nodiscard]] TFuture<void> Finish() &&;
  49. private:
  50. NWaitGroup::NImpl::TStateRef<WaitPolicy> State_;
  51. };
  52. }
  53. #define INCLUDE_FUTURE_INL_H
  54. #include "wait_group-inl.h"
  55. #undef INCLUDE_FUTURE_INL_H