#include <xtd/xtd>
namespace manual_reset_event_example {
class program {
public:
static void main() {
console::write_line("\nStart 3 named threads that block on a ManualresetEvent:\n");
for(auto i = 0; i <= 2; ++i) {
threads.emplace_back(thread_proc);
threads.back().name(string::format("Thread_{}", i));
threads.back().start();
}
thread::sleep(500);
console::write_line("\nWhen all three threads have started, press Enter to call set()"
"\nto release all the threads.\n");
console::read_line();
mre.set();
thread::sleep(500);
console::write_line("\nWhen a ManualresetEvent is signaled, threads that call WaitOne()"
"\ndo not block. Press Enter to show this.\n");
console::read_line();
for(auto i = 3; i <= 4; ++i) {
threads.emplace_back(thread_proc);
threads.back().name(string::format("Thread_{}", i));
threads.back().start();
}
thread::sleep(500);
console::write_line("\nPress Enter to call reset(), so that threads once again block"
"\nwhen they call WaitOne().\n");
console::read_line();
mre.reset();
threads.emplace_back(thread_proc);
threads.back().name("Thread_5");
threads.back().start();
thread::sleep(500);
console::write_line("\nPress Enter to call set() and conclude the demo.");
console::read_line();
mre.set();
}
private:
inline static list<thread> threads = list<thread>(4);
inline static manual_reset_event mre {false};
static void thread_proc() {
string name = thread::current_thread().name();
console::write_line(name + " starts and calls mre.WaitOne()");
mre.wait_one();
console::write_line(name + " ends.");
}
};
}
startup_(manual_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