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

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

#include <xtd/threading/barrier>
#include <xtd/threading/interlocked>
#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() {
auto count = 0;
// Create a barrier with three participants
// Provide a post-phase action that will print out certain information
// And the third time through, it will throw an exception
console::write_line("Post-Phase action: count={0}, phase={1}", count, b.current_phase_number());
if (b.current_phase_number() == 2) throw system_exception("D'oh!");
}}};
// Nope -- changed my mind. Let's make it five participants.
// Nope -- let's settle on four participants.
auto action = [&] {
interlocked::increment(count);
barrier.signal_and_wait(); // during the post-phase action, count should be 4 and phase should be 0
interlocked::increment(count);
barrier.signal_and_wait(); // during the post-phase action, count should be 8 and phase should be 1
// The third time, SignalAndWait() will throw an exception and all participants will see it
interlocked::increment(count);
try {
} catch (const barrier_post_phase_exception& bppe) {
console::write_line("Caught barrier_post_phase_exception: {0}", bppe.message());
}
// The fourth time should be hunky-dory
interlocked::increment(count);
barrier.signal_and_wait(); // during the post-phase action, count should be 16 and phase should be 3
};
// Now launch 4 parallel actions to serve as 4 participants
for (auto index = 0; index < 4; ++index)
thread_pool::queue_user_work_item(action);
// It's good form to join all thread and close() a barrier when you're done with it.
thread::join_all();
}
};
}
startup_(barrier_example::program::main);
// This example produces output similar to the following:
//
// Post-Phase action: count=4, phase=0
// Post-Phase action: count=8, phase=1
// Post-Phase action: count=12, phase=2
// Post-Phase action: count=16, phase=3
virtual const xtd::string & message() const noexcept
Gets message associate to the exception.
The exception that is thrown when a method call is invalid for the object's current state.
Definition system_exception.h:18
The exception that is thrown when a Thread is in an invalid ThreadState for the method call.
Definition barrier_post_phase_exception.h:28
Enables multiple tasks to cooperatively work on an algorithm in parallel through multiple phases.
Definition barrier.h:31
int32 remove_participant()
Notifies the Barrier that there will be one less participant.
int32 add_participants(int32 participant_count)
Notifies the xtd::threading::barrier that there will be additional participants.
void close()
Close the current instance of the xtd::threading::barrier class.
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:175
delegate< void(arguments_t...)> action
Represents a xtd::delegate that has variable parameters and does not return a value.
Definition action.h:20
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