CppCommon  1.0.4.1
C++ Common Library
threads_named_event_manual_reset.cpp

Named manual-reset event synchronization primitive example

#include "threads/thread.h"
#include <iostream>
#include <thread>
#include <vector>
int main(int argc, char** argv)
{
std::string help = "Please enter '!' to signal the named manual-reset event (several processes support). Enter '0' to exit...";
// Show help message
std::cout << help << std::endl;
int concurrency = 8;
// Named manual-reset event master
CppCommon::NamedEventManualReset event_master("named_manual_event_example");
// Start some threads
std::vector<std::thread> threads;
for (int thread = 0; thread < concurrency; ++thread)
{
threads.emplace_back([thread]()
{
// Named manual-reset event slave
CppCommon::NamedEventManualReset event_slave("named_manual_event_example");
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_slave.Wait();
std::cout << "Thread " << thread << " signaled!" << std::endl;
});
}
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line == "!")
{
std::cout << "Signal event!" << std::endl;
event_master.Signal();
}
else if (line == "0")
break;
else
std::cout << help << std::endl;
}
// Wait for all threads
for (auto& thread : threads)
thread.join();
return 0;
}
Named manual-reset event synchronization primitive.
void Signal()
Signal one of waiting thread about event occurred.
void Wait()
Try to wait the event with block.
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
Named manual-reset event synchronization primitive definition.
Thread definition.