From 7a91597392c64ec8bd2b3cdfc44df98b928f41d3 Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Wed, 8 Nov 2023 00:14:42 +0100 Subject: latch: add simple latch based on a mutex and cv --- test/latch.cc | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/latch.cc (limited to 'test') diff --git a/test/latch.cc b/test/latch.cc new file mode 100644 index 0000000..567e3db --- /dev/null +++ b/test/latch.cc @@ -0,0 +1,37 @@ +#include + +#include +#include +#include + +#include +#include + +constexpr unsigned kNumThreads = 16; + +int main() { + latch gate(kNumThreads); + + std::vector threads; + threads.reserve(kNumThreads); + + for (unsigned t = 0; t < kNumThreads; ++t) { + threads.emplace_back([&gate, t]() { + if (t % 2 == 0) { + unsigned sec = t / 2; + std::printf("th%02u sleep %us\n", t, sec); + std::this_thread::sleep_for(std::chrono::seconds(sec)); + } + + std::printf("th%02u at gate\n", t); + gate.arrive_and_wait(); + std::printf("th%02u finished\n", t); + }); + } + + for (auto& th : threads) { + th.join(); + } + + return 0; +} -- cgit v1.2.3