xtd 0.2.0
Loading...
Searching...
No Matches
auto_reset_event.cpp

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

#include <xtd/collections/generic/list>
#include <xtd/threading/auto_reset_event>
#include <xtd/threading/thread>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::collections::generic;
using namespace xtd::threading;
namespace auto_reset_event_example {
class program {
public:
// The main entry point for the application.
static auto main() {
console::write_line("Press Enter to create three threads and start them.\r\n"
"The threads wait on auto_reset_event #1, which was created\r\n"
"in the signaled state, so the first thread is released.\r\n"
"This puts auto_reset_event #1 into the unsignaled state.");
console::read_line();
for (auto index = 1; index < 4; ++index) {
threads.emplace_back(thread_proc);
threads.back().name(string::format("Thread_{}", index));
threads.back().start();
}
thread::sleep(250_ms);
for (auto index = 0; index < 2; ++index) {
console::write_line("Press Enter to release another thread.");
console::read_line();
event_1.set();
thread::sleep(250_ms);
}
console::write_line("\r\nAll threads are now waiting on auto_reset_event #2.");
for (int i = 0; i < 3; i++) {
console::write_line("Press Enter to release a thread.");
console::read_line();
event_2.set();
thread::sleep(250_ms);
}
}
static void thread_proc() {
string name = thread::current_thread().name();
console::write_line("{0} waits on auto_reset_event #1.", name);
event_1.wait_one();
console::write_line("{0} is released from auto_reset_event #1.", name);
console::write_line("{0} waits on auto_reset_event #2.", name);
event_2.wait_one();
console::write_line("{0} is released from auto_reset_event #2.", name);
console::write_line("{0} ends.", name);
}
private:
inline static list<thread> threads = list<thread>(4);
inline static auto_reset_event event_1 {true};
inline static auto_reset_event event_2 {false};
};
}
startup_(auto_reset_event_example::program::main);
// This example produces output similar to the following:
//
// Press Enter to create three threads and start them.
// The threads wait on auto_reset_event #1, which was created
// in the signaled state, so the first thread is released.
// This puts auto_reset_event #1 into the unsignaled state.
//
// Thread_1 waits on auto_reset_event #1.
// Thread_1 is released from auto_reset_event #1.
// Thread_1 waits on auto_reset_event #2.
// Thread_2 waits on auto_reset_event #1.
// Thread_3 waits on auto_reset_event #1.
// Press Enter to release another thread.
//
// Thread_3 is released from auto_reset_event #1.
// Thread_3 waits on auto_reset_event #2.
// Press Enter to release another thread.
//
// Thread_2 is released from auto_reset_event #1.
// Thread_2 waits on auto_reset_event #2.
//
// All threads are now waiting on auto_reset_event #2.
// Press Enter to release a thread.
//
// Thread_1 is released from auto_reset_event #2.
// Thread_1 ends.
// Press Enter to release a thread.
//
// Thread_3 is released from auto_reset_event #2.
// Thread_3 ends.
// Press Enter to release a thread.
//
// Thread_2 is released from auto_reset_event #2.
// Thread_2 ends.
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:71
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::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:15
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