Spin barrier synchronization primitive example
#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([&barrier, thread]()
{
std::cout << "Thread " << thread << " initialized!" << std::endl;
std::cout << "Thread " << thread << " before barrier!" << std::endl;
bool last = barrier.
Wait();
std::cout << "Thread " << thread << " after barrier!" << (last ? " Last one!" : "") << std::endl;
});
}
for (auto& thread : threads)
thread.join();
return 0;
}
Spin barrier synchronization primitive.
bool Wait() noexcept
Wait at the barrier until all other threads reach this barrier.
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.
Spin barrier synchronization primitive definition.