1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-02 09:37:57 +01:00
parallel/examples/thread.php
2015-08-03 15:09:26 -05:00

29 lines
713 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 = new ThreadContext(function () {
print "Sleeping for 5 seconds...\n";
sleep(5);
});
// Run the thread and wait asynchronously for it to finish.
$test->start();
yield $test->join();
print "Thread ended!\n";
})->done(function () {
Loop\stop();
});
Loop\run();