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
713 B
PHP
Raw Normal View History

2015-07-14 00:30:59 +02:00
<?php
require dirname(__DIR__).'/vendor/autoload.php';
2015-07-14 00:30:59 +02:00
use Icicle\Concurrent\Threading\ThreadContext;
use Icicle\Coroutine;
2015-07-14 00:30:59 +02:00
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";
});
2015-07-14 00:30:59 +02:00
// Create a new child thread that does some blocking stuff.
$test = new ThreadContext(function () {
print "Sleeping for 5 seconds...\n";
sleep(5);
});
2015-07-27 00:53:00 +02:00
// Run the thread and wait asynchronously for it to finish.
$test->start();
yield $test->join();
2015-07-14 00:30:59 +02:00
print "Thread ended!\n";
})->done(function () {
2015-07-14 00:30:59 +02:00
Loop\stop();
});
Loop\run();