#include <xtd/threading/event_wait_handle>
#include <xtd/threading/interlocked>
#include <xtd/threading/thread>
#include <xtd/console>
#include <xtd/startup>
class example {
private:
inline static int64 thread_count = 0;
public:
static void main() {
for (auto i = 0; i <= 4; i++) {
threads_[i] =
thread {thread_proc};
}
while (interlocked::read(thread_count) < 5) {
thread::sleep(500);
}
while (interlocked::read(thread_count) > 0) {
console::write_line("Press ENTER to release a waiting thread.");
console::read_line();
wait_handle::signal_and_wait(ewh, clear_count);
}
console::write_line();
for(auto i = 0; i <= 4; i++) {
threads_[i] =
thread {thread_proc};
}
while (interlocked::read(thread_count) < 5) {
thread::sleep(500);
}
console::write_line("Press ENTER to release the waiting threads.");
console::read_line();
}
private:
static void thread_proc(std::any data) {
console::write_line("Thread {0} blocks.", as<int>(data));
interlocked::increment(thread_count);
console::write_line("Thread {0} exits.", as<int>(data));
interlocked::decrement(thread_count);
clear_count.set();
}
inline static std::array<thread, 5> threads_;
};
Represents a thread synchronization event.
Definition event_wait_handle.h:37
bool set()
Sets the state of the event to signaled, allowing one or more waiting threads to proceed.
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.h:43
void start()
Causes the operating system to change the state of the current instance to xtd::threading::thread_sta...
virtual bool wait_one()
Blocks the current thread until the current xtd::threading::wait_handle receives a signal.
#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
int64_t int64
Represents a 64-bit signed integer.
Definition int64.h:23
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