Parallelism:-
Most modern microprocessors consist of more than one core, each of which can operate as an individual processing unit. They can execute different parts of different programs at the same time. The features of the std.parallelism module make it possible for programs to take advantage of all of the cores in order to run faster.
parallel: Accesses the elements of a range in parallel
task: Creates tasks that are executed in parallel.
map: Calls functions with the elements of an InputRange semi-eagerly in parallel.
amap: Calls functions with the elements of a RandomAccessRange fully-eagerly in parallel.
reduce: Makes calculations over the elements of a RandomAccessRange in parallel.
import std.stdio;
import core.thread;
struct Player {
int number;
void parallel_op() {
writefln("match %s has begun", number);
// Wait for a while to simulate a long-lasting operation
Thread.sleep(2.seconds);
writefln("match %s has ended", number);
}
}
void main() {
auto players =
[ Player(1), Player(2), Player(3),Player(4) ];
foreach (player; players) {
player.parallel_op();
}
}