xtd 0.2.0
manual_reset_event.cpp

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

#include <xtd/threading/manual_reset_event>
#include <xtd/threading/thread>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::threading;
namespace manual_reset_event_example {
class program {
public:
static void main() {
console::write_line("\nStart 3 named threads that block on a ManualresetEvent:\n");
for(auto i = 0; i <= 2; ++i) {
threads.emplace_back(thread_proc);
threads.back().name(ustring::format("Thread_{}", i));
threads.back().start();
}
console::write_line("\nWhen all three threads have started, press Enter to call set()"
"\nto release all the threads.\n");
mre.set();
console::write_line("\nWhen a ManualresetEvent is signaled, threads that call WaitOne()"
"\ndo not block. Press Enter to show this.\n");
for(auto i = 3; i <= 4; ++i) {
threads.emplace_back(thread_proc);
threads.back().name(ustring::format("Thread_{}", i));
threads.back().start();
}
console::write_line("\nPress Enter to call reset(), so that threads once again block"
"\nwhen they call WaitOne().\n");
mre.reset();
// Start a thread that waits on the ManualresetEvent.
threads.emplace_back(thread_proc);
threads.back().name("Thread_5");
threads.back().start();
console::write_line("\nPress Enter to call set() and conclude the demo.");
mre.set();
thread::join_all(threads);
}
private:
inline static std::vector<thread> threads = std::vector<thread>(4);
// mre is used to block and release threads manually. It is
// created in the unsignaled state.
inline static manual_reset_event mre {false};
static void thread_proc() {
console::write_line(name + " starts and calls mre.WaitOne()");
mre.wait_one();
console::write_line(name + " ends.");
}
};
}
startup_(manual_reset_event_example::program::main);
// This example produces output similar to the following:
//
// Start 3 named threads that block on a ManualresetEvent:
//
// Thread_0 starts and calls mre.WaitOne()
// Thread_2 starts and calls mre.WaitOne()
// Thread_1 starts and calls mre.WaitOne()
//
// When all three threads have started, press Enter to call set()
// to release all the threads.
//
//
// Thread_2 ends.
// Thread_0 ends.
// Thread_1 ends.
//
// When a ManualresetEvent is signaled, threads that call WaitOne()
// do not block. Press Enter to show this.
//
//
// Thread_3 starts and calls mre.WaitOne()
// Thread_4 starts and calls mre.WaitOne()
// Thread_3 ends.
// Thread_4 ends.
//
// Press Enter to call reset(), so that threads once again block
// when they call WaitOne().
//
//
// Thread_5 starts and calls mre.WaitOne()
//
// Press Enter to call set() and conclude the demo.
//
// Thread_5 ends.