<?php
use parallel\{Future, Runtime};
const THREADS_COUNT = 10;
const THREADS_I_MAX = 100;
$task = static function (int $i, int $to): void {
echo "[enter$i]";
for ($j = 0; $j < $to; $j++) {
echo $i;
}
echo "[exit$i]";
};
$runtimeList = [];
for ($i = 0; $i < THREADS_COUNT; $i++) {
$runtimeList[] = new Runtime();
}
$futureList = [];
foreach ($runtimeList as $i => $runtime) {
echo "[run$i]";
$futureList[] = $runtime->run($task, [$i, THREADS_I_MAX]);
}
do {
usleep(1);
$allDone = array_reduce(
$futureList,
function (bool $c, Future $future): bool {
return $c && $future->done();
},
true
);
} while (false === $allDone);
echo "done\n";