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

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

#include <xtd/threading/barrier>
#include <xtd/threading/thread_pool>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::threading;
namespace barrier_example {
class program {
public:
static void main() {
for (auto index = 0; index < task_count; ++index)
thread_pool::queue_user_work_item(perform_phase);
}
private:
static void perform_phase() {
console::write_line("Thread {} is performing its work", thread::current_thread().managed_thread_id());
console::write_line("Thread {} compelted the phase", thread::current_thread().managed_thread_id());
}
static constexpr int task_count = 3;
inline static threading::barrier barrier {task_count};
};
}
startup_(barrier_example::program::main);
// This example produces output similar to the following:
//
// Thread 9 is performing its work
// Thread 11 is performing its work
// Thread 8 is performing its work
// Thread 11 compelted the phase
// Thread 8 compelted the phase
// Thread 9 compelted the phase
Enables multiple tasks to cooperatively work on an algorithm in parallel through multiple phases.
Definition barrier.h:29
void signal_and_wait()
Signals that a participant has reached the barrier and waits for all other participants to reach the ...
#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:166
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.h:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10