#include <xtd/collections/generic/list>
#include <xtd/threading/interlocked>
#include <xtd/threading/lock_guard>
#include <xtd/threading/thread>
#include <xtd/console>
#include <xtd/literals>
#include <xtd/random>
#include <xtd/startup>
namespace lock_guard_example {
class program {
public:
static void main() {
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;
for (ctr = 0; ctr < 10000; ++ctr)
values[ctr] = rnd.next(0, 1001);
}
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 {
console::write_line("\nMean for all tasks: {0:N2} (N={1:N0})",
(total * 1.0)/n, n);
}
console::write_line("{0}: {1}", e.get_type().name(), e.message());
}
}
};
}
startup_(lock_guard_example::program::main);
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:71
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet c...
Definition random.h:40
The exception that is thrown when a method call is invalid for the object's current state.
Definition system_exception.h:18
Provides a mechanism that synchronizes access to objects with xtd::threading::monitor.
Definition lock_guard.h:32
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.h:43
void join()
Blocks the calling thread until this thread object terminates, while continuing to perform standard C...
#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
#define block_scope_(...)
The specified expression is cleared automatically when the scope is ended.
Definition block_scope.h:25
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:15
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