#include <xtd/xtd>
namespace monitor_example {
class program {
public:
static void main() {
auto threads = list<thread> {};
auto total = 0_s64;
auto n = 0;
for (auto thread_ctr = 0; thread_ctr < 10; ++thread_ctr)
threads.push_back(thread::start_new([&] {
auto values = std::array<int, 10000> {};
auto thread_total = 0;
auto thread_n = 0;
auto ctr = 0;
monitor::enter(rnd);
for (ctr = 0; ctr < 10000; ++ctr)
values[ctr] = rnd.next(0, 1001);
monitor::exit(rnd);
thread_n = ctr;
for (auto value : values)
thread_total += value;
console::write_line("Mean for task {0,2}: {1:N2} (N={2:N0})",
thread::current_thread().managed_thread_id(),
(thread_total * 1.0) / thread_n, thread_n);
interlocked::add(n, thread_n);
interlocked::add(total, thread_total);
}));
try {
for (auto& thread : threads)
thread.join();
console::write_line("\nMean for all tasks: {0:N2} (N={1:N0})",
(total * 1.0)/n, n);
}
catch (const system_exception& e) {
console::write_line("{0}: {1}", e.get_type().name(), e.message());
}
}
};
}
startup_(monitor_example::program::main);
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet c...
Definition random.hpp:44
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:168