2015-08-06 18:59:25 -05:00
|
|
|
#!/usr/bin/env php
|
2015-07-13 17:30:59 -05:00
|
|
|
<?php
|
2015-08-03 15:09:26 -05:00
|
|
|
require dirname(__DIR__).'/vendor/autoload.php';
|
2015-07-13 17:30:59 -05:00
|
|
|
|
|
|
|
use Icicle\Concurrent\Threading\ThreadContext;
|
2015-08-03 15:09:26 -05:00
|
|
|
use Icicle\Coroutine;
|
2015-07-13 17:30:59 -05:00
|
|
|
use Icicle\Loop;
|
|
|
|
|
2015-08-06 18:59:25 -05:00
|
|
|
$timer = Loop\periodic(1, function () {
|
2015-08-07 15:30:14 -05:00
|
|
|
static $i;
|
|
|
|
$i = $i ? ++$i : 1;
|
|
|
|
print "Demonstrating how alive the parent is for the {$i}th time.\n";
|
2015-08-06 18:59:25 -05:00
|
|
|
});
|
|
|
|
|
2015-08-03 15:09:26 -05:00
|
|
|
Coroutine\create(function () {
|
|
|
|
// Create a periodic message in the main thread.
|
2015-07-13 17:30:59 -05:00
|
|
|
|
2015-08-03 15:09:26 -05:00
|
|
|
// Create a new child thread that does some blocking stuff.
|
2015-08-07 15:30:14 -05:00
|
|
|
$context = new ThreadContext(function () {
|
|
|
|
print "Sleeping for 3 seconds...\n";
|
|
|
|
sleep(3);
|
|
|
|
|
|
|
|
yield $this->send('Data sent from child.');
|
|
|
|
|
|
|
|
print "Sleeping for 2 seconds...\n";
|
|
|
|
sleep(2);
|
|
|
|
|
|
|
|
yield 42;
|
2015-08-03 15:09:26 -05:00
|
|
|
});
|
2015-07-26 17:53:00 -05:00
|
|
|
|
2015-08-03 15:09:26 -05:00
|
|
|
// Run the thread and wait asynchronously for it to finish.
|
2015-08-07 15:30:14 -05:00
|
|
|
$context->start();
|
|
|
|
|
|
|
|
printf("Received the following from child: %s\n", (yield $context->receive()));
|
|
|
|
printf("Thread ended with value %d!\n", (yield $context->join()));
|
|
|
|
})->cleanup([$timer, 'stop']);
|
2015-07-13 17:30:59 -05:00
|
|
|
|
|
|
|
Loop\run();
|