#include <xtd/xtd>
class example {
private:
inline static event_wait_handle ewh;
inline static int64 thread_count = 0;
inline static event_wait_handle clear_count {false, event_reset_mode::auto_reset};
public:
static void main() {
ewh = event_wait_handle {false, event_reset_mode::auto_reset};
for (auto i = 0; i <= 4; i++) {
threads[i] = thread {thread_proc};
threads[i].start(i);
}
while (interlocked::read(thread_count) < 5) {
thread::sleep(500);
}
while (interlocked::read(thread_count) > 0) {
console::write_line("Press ENTER to release a waiting thread.");
console::read_line();
wait_handle::signal_and_wait(ewh, clear_count);
}
console::write_line();
ewh = event_wait_handle(false, event_reset_mode::manual_reset);
for(auto i = 0; i <= 4; i++) {
threads[i] = thread {thread_proc};
threads[i].start(i);
}
while (interlocked::read(thread_count) < 5) {
thread::sleep(500);
}
console::write_line("Press ENTER to release the waiting threads.");
console::read_line();
ewh.set();
}
private:
static void thread_proc(any_object data) {
console::write_line("Thread {0} blocks.", as<int>(data));
interlocked::increment(thread_count);
ewh.wait_one();
console::write_line("Thread {0} exits.", as<int>(data));
interlocked::decrement(thread_count);
clear_count.set();
}
inline static std::array<thread, 5> threads;
};
#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