xtd 0.2.0
Loading...
Searching...
No Matches
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);
thread::sleep(1000);
lock_("lock") {
monitor::pulse("lock");
}
thread::sleep(1000);
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.
#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:175
#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:11
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10