Latch synchronization primitive example for multiple threads waiting
#include <iostream>
#include <thread>
#include <vector>
int main(int argc, char** argv)
{
int concurrency = 8;
std::vector<std::thread> threads;
for (int thread = 0; thread < concurrency; ++thread)
{
threads.emplace_back([&latch, thread]()
{
std::cout << "Thread " << thread << " initialized!" << std::endl;
std::cout << "Thread " << thread << " latch count down!" << std::endl;
});
}
std::cout << "Main thread is waiting for the latch..." << std::endl;
std::cout << "Main thread continue!" << std::endl;
for (auto& thread : threads)
thread.join();
return 0;
}
Latch synchronization primitive.
void CountDown() noexcept
Countdown the latch.
void Wait() noexcept
Wait for the latch.
static void SleepFor(const Timespan ×pan) noexcept
Sleep the current thread for the given timespan.
int64_t milliseconds() const noexcept
Get total milliseconds of the current timespan.
Latch synchronization primitive definition.