An implementation of a count down latch that returns a Future to signify when it gets triggered.
| 22 | // An implementation of a count down latch that returns a Future to |
| 23 | // signify when it gets triggered. |
| 24 | class CountDownLatch |
| 25 | { |
| 26 | public: |
| 27 | CountDownLatch(size_t count = 1) : count(count) {} |
| 28 | |
| 29 | void decrement() |
| 30 | { |
| 31 | size_t expected = count.load(); |
| 32 | while (expected > 0) { |
| 33 | if (count.compare_exchange_strong(expected, expected - 1)) { |
| 34 | if (expected == 1) { |
| 35 | promise.set(Nothing()); |
| 36 | } |
| 37 | break; |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | Future<Nothing> triggered() |
| 43 | { |
| 44 | return promise.future(); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | std::atomic<size_t> count; |
| 49 | Promise<Nothing> promise; |
| 50 | }; |
| 51 | |
| 52 | } // namespace process { |
| 53 |
nothing calls this directly
no outgoing calls
no test coverage detected