#include <xtd/threading/auto_reset_event>
#include <xtd/threading/thread_pool>
#include <xtd/console>
#include <xtd/date_time>
#include <xtd/random>
#include <xtd/startup>
namespace wait_handle_example {
class program {
public:
static void main() {
auto dt = date_time::now();
console::write_line("Main thread is waiting for BOTH tasks to complete.");
thread_pool::queue_user_work_item(do_task, event1);
thread_pool::queue_user_work_item(do_task, event2);
wait_handle::wait_all({event1, event2});
console::write_line("Both tasks are completed (time waited={0})",
date_time::now() - dt);
dt = date_time::now();
console::write_line();
console::write_line("The main thread is waiting for either task to complete.");
thread_pool::queue_user_work_item(do_task, event1);
thread_pool::queue_user_work_item(do_task, event2);
auto index = wait_handle::wait_any({event1, event2});
console::write_line("Task {0} finished first (time waited={1}).",
index + 1, date_time::now() - dt);
}
static void do_task(std::any state) {
auto are = as<auto_reset_event>(state);
auto time = 1000 * r.next(2, 10);
console::write_line("Performing a task for {0} milliseconds.", time);
thread::sleep(time);
are.set();
}
private:
};
}
startup_(wait_handle_example::program::main);
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet c...
Definition random.h:40
Represents a thread synchronization event that, when signaled, resets automatically after releasing a...
Definition auto_reset_event.h:36
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.h:11
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10