1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-03 10:07:49 +01:00
parallel/examples/thread.php

29 lines
750 B
PHP

<?php
require dirname(__DIR__).'/vendor/autoload.php';
use Icicle\Concurrent\Threading\ThreadContext;
use Icicle\Coroutine;
use Icicle\Loop;
Coroutine\create(function () {
// Create a periodic message in the main thread.
$timer = Loop\periodic(1, function () {
print "Demonstrating how alive the parent is.\n";
});
// Create a new child thread that does some blocking stuff.
$test = ThreadContext::create(function () {
print "Sleeping for 5 seconds...\n";
sleep(5);
return 42;
});
// Run the thread and wait asynchronously for it to finish.
$test->start();
printf("Thread ended with value %d!\n", (yield $test->join()));
})->done(function () {
Loop\stop();
});
Loop\run();