#include <xtd/xtd>
namespace auto_reset_event_example {
class program {
public:
static auto main() {
console::write_line("Press Enter to create three threads and start them.\r\n"
"The threads wait on auto_reset_event #1, which was created\r\n"
"in the signaled state, so the first thread is released.\r\n"
"This puts auto_reset_event #1 into the unsignaled state.");
console::read_line();
for (auto index = 1; index < 4; ++index) {
threads.emplace_back(thread_proc);
threads.back().name(string::format("Thread_{}", index));
threads.back().start();
}
thread::sleep(250_ms);
for (auto index = 0; index < 2; ++index) {
console::write_line("Press Enter to release another thread.");
console::read_line();
event_1.set();
thread::sleep(250_ms);
}
console::write_line("\r\nAll threads are now waiting on auto_reset_event #2.");
for (int i = 0; i < 3; i++) {
console::write_line("Press Enter to release a thread.");
console::read_line();
event_2.set();
thread::sleep(250_ms);
}
thread::join_all(threads);
}
static void thread_proc() {
string name = thread::current_thread().name();
console::write_line("{0} waits on auto_reset_event #1.", name);
event_1.wait_one();
console::write_line("{0} is released from auto_reset_event #1.", name);
console::write_line("{0} waits on auto_reset_event #2.", name);
event_2.wait_one();
console::write_line("{0} is released from auto_reset_event #2.", name);
console::write_line("{0} ends.", name);
}
private:
inline static list<thread> threads = list<thread>(4);
inline static auto_reset_event event_1 {true};
inline static auto_reset_event event_2 {false};
};
}
startup_(auto_reset_event_example::program::main);
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:168