2015-08-07 01:59:25 +02:00
|
|
|
#!/usr/bin/env php
|
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-07 01:59:25 +02:00
|
|
|
$timer = Loop\periodic(1, function () {
|
|
|
|
print "Demonstrating how alive the parent is.\n";
|
|
|
|
});
|
|
|
|
|
2015-08-03 22:09:26 +02:00
|
|
|
Coroutine\create(function () {
|
|
|
|
// Create a periodic message in the main thread.
|
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.
|
2015-08-07 01:59:25 +02:00
|
|
|
$test = new ThreadContext(function () {
|
2015-08-03 22:09:26 +02:00
|
|
|
print "Sleeping for 5 seconds...\n";
|
|
|
|
sleep(5);
|
2015-08-05 20:29:11 +02:00
|
|
|
return 42;
|
2015-08-03 22:09:26 +02:00
|
|
|
});
|
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();
|
2015-08-05 20:29:11 +02:00
|
|
|
printf("Thread ended with value %d!\n", (yield $test->join()));
|
2015-08-07 01:59:25 +02:00
|
|
|
})->done([$timer, 'stop']);
|
2015-07-14 00:30:59 +02:00
|
|
|
|
|
|
|
Loop\run();
|