basic-multithreading-test.php
<?php
use parallel\Runtime;
use parallel\Channel;
$test = "this var is not accesible in a thread";
$thread_function = function (int $id, Channel $ch) {
$sleep = ($id == 2) ? 1 : 2;
sleep($sleep);
var_dump("thread $id sleep $sleep");
echo '$GLOBALS["test"] = ';
@var_dump($GLOBALS["test"]);
$ch->send($sleep);
};
try {
$r1 = new Runtime();
$r2 = new Runtime();
$ch1 = new Channel();
$args = array();
$args[0] = null;
$args[1] = $ch1;
$args[0] = 1;
$r1->run($thread_function, $args);
$args[0] = 2;
$r2->run($thread_function, $args);
$x = $ch1->recv();
$y = $ch1->recv();
$ch1->close();
echo "\nData received by the channel: $x and $y";
} catch (Error $err) {
echo "\nError:", $err->getMessage();
} catch (Exception $e) {
echo "\nException:", $e->getMessage();
}