xtd 0.2.0
monitor_pulse.cpp

Shows how to use xtd::threading::monitor::pulse pulse.

#include <xtd/threading/monitor>
#include <xtd/threading/thread>
#include <xtd/console>
#include <xtd/lock>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::threading;
namespace monitor_lock_example {
class program {
public:
static void main() {
auto t1 = thread::start_new(worker);
auto t2 = thread::start_new(worker);
lock_("lock") {
monitor::pulse("lock");
}
lock_("lock") {
monitor::pulse("lock");
}
thread::join_all({t1, t2});
}
private:
static void worker() {
lock_("lock") {
console::write_line(" Thread {} wait...", thread::current_thread().managed_thread_id());
monitor::wait("lock");
console::write_line(" Thread {} ...continue.", thread::current_thread().managed_thread_id());
}
}
};
}
startup_(monitor_lock_example::program::main);
// This example produces output similar to the following:
//
// Thread 2 wait...
// Thread 3 wait...
// Thread 3 ...continue.
// Thread 2 ...continue.
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
static void pulse(const object_t &obj)
Notifies a thread in the waiting queue of a change in the locked object's state.
Definition monitor.hpp:209
static bool wait(const object_t &obj, int32 milliseconds_timeout)
Releases the lock on an object and blocks the current thread until it reacquires the lock....
Definition monitor.hpp:378
static thread & current_thread() noexcept
Gets the currently running thread.
static thread start_new(const xtd::threading::thread_start &start)
Create and immedialtely start a xtd::threading::thread with specified method.
static void sleep(int32 milliseconds_timeout)
Suspends the current thread for a specified time.
static void join_all()
Blocks the calling thread until all joinable threads terminate.
#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
#define lock_(object)
The lock_ keyword marks a statement block as a critical section by obtaining the mutual-exclusion loc...
Definition lock.hpp:85
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