2015-07-14 00:30:59 +02:00
|
|
|
<?php
|
2015-08-03 22:09:26 +02:00
|
|
|
require dirname(__DIR__).'/vendor/autoload.php';
|
2015-07-14 00:30:59 +02:00
|
|
|
|
|
|
|
use Icicle\Concurrent\Threading\ThreadContext;
|
2015-08-03 22:09:26 +02:00
|
|
|
use Icicle\Coroutine;
|
2015-07-14 00:30:59 +02:00
|
|
|
use Icicle\Loop;
|
|
|
|
|
2015-08-03 22:09:26 +02:00
|
|
|
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
|
|
|
|
2015-08-03 22:09:26 +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
|
|
|
|
2015-08-03 22:09:26 +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";
|
2015-08-03 22:09:26 +02:00
|
|
|
})->done(function () {
|
2015-07-14 00:30:59 +02:00
|
|
|
Loop\stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
Loop\run();
|