CppCommon  1.0.4.1
C++ Common Library
threads_event_auto_reset.cpp

Auto-reset event synchronization primitive example

#include "threads/thread.h"
#include <iostream>
#include <thread>
#include <vector>
int main(int argc, char** argv)
{
int concurrency = 8;
// Start some threads
std::vector<std::thread> threads;
for (int thread = 0; thread < concurrency; ++thread)
{
threads.emplace_back([&event, thread]()
{
std::cout << "Thread " << thread << " initialized!" << std::endl;
// Sleep for a while...
std::cout << "Thread " << thread << " waiting for the event!" << std::endl;
// Wait for the event
event.Wait();
std::cout << "Thread " << thread << " signaled!" << std::endl;
});
}
// Allow threads to start
// Signal the event for each thread that waits
for (int thread = 0; thread < concurrency; ++thread)
{
std::cout << "Signal event!" << std::endl;
event.Signal();
}
// Wait for all threads
for (auto& thread : threads)
thread.join();
return 0;
}
Auto-reset event synchronization primitive.
static void SleepFor(const Timespan &timespan) noexcept
Sleep the current thread for the given timespan.
Definition: thread.cpp:83
int64_t milliseconds() const noexcept
Get total milliseconds of the current timespan.
Definition: timespan.h:141
Auto-reset event synchronization primitive definition.
Thread definition.