xtd 0.2.0
wait_handle.cpp

Shows how to use xtd::threading::wait_handle class.

#include <xtd/threading/auto_reset_event>
#include <xtd/threading/thread_pool>
#include <xtd/console>
#include <xtd/date_time>
#include <xtd/random>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::threading;
namespace wait_handle_example {
class program {
public:
static void main() {
// Queue up two tasks on two different threads;
// wait until all tasks are completed.
auto dt = date_time::now();
console::write_line("Main thread is waiting for BOTH tasks to complete.");
wait_handle::wait_all({event1, event2});
// The time shown below should match the longest task.
console::write_line("Both tasks are completed (time waited={0})",
date_time::now() - dt);
// Queue up two tasks on two different threads;
// wait until any task is completed.
console::write_line("The main thread is waiting for either task to complete.");
auto index = wait_handle::wait_any({event1, event2});
// The time shown below should match the shortest task.
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);
are.set();
}
private:
// Define two auto_reset_event.
inline static auto_reset_event event1 {false};
inline static auto_reset_event event2 {false};
// Define a random number generator for testing.
inline static xtd::random r;
};
}
startup_(wait_handle_example::program::main);
// This example produces output similar to the following:
//
// Main thread is waiting for BOTH tasks to complete.
// Performing a task for 4000 milliseconds.
// Performing a task for 9000 milliseconds.
// Both tasks are completed (time waited=00:00:09:011011000)
//
// The main thread is waiting for either task to complete.
// Performing a task for 7000 milliseconds.
// Performing a task for 4000 milliseconds.
// Task 2 finished first (time waited=00:00:04:011925000).
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
static date_time now() noexcept
Gets a xtd::date_time object that is set to the current date and time on this computer,...
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet c...
Definition random.hpp:44
Represents a thread synchronization event that, when signaled, resets automatically after releasing a...
Definition auto_reset_event.hpp:36
static bool queue_user_work_item(const wait_callback &callback)
Queues a method for execution. The method executes when a thread pool thread becomes available.
static void sleep(int32 milliseconds_timeout)
Suspends the current thread for a specified time.
static bool wait_all(const collection_t &wait_handles)
Waits for all the elements in the specified collection to receive a signal.
Definition wait_handle.hpp:161
static size_t wait_any(const collection_t &wait_handles)
Waits for any of the elements in the specified collection to receive a signal.
Definition wait_handle.hpp:198
#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:167
type_t as(any_object &o)
Casts a type into another type.
Definition __as_any_object.hpp:59
@ r
The R key.
Definition console_key.hpp:122
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8