1
0
mirror of https://github.com/danog/parallel.git synced 2025-01-22 05:51:14 +01:00

Update thread example to use coroutines

This commit is contained in:
coderstephen 2015-08-03 15:09:26 -05:00
parent c110d4baa6
commit e388e08f4f

View File

@ -1,24 +1,27 @@
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
require dirname(__DIR__).'/vendor/autoload.php';
use Icicle\Concurrent\Threading\ThreadContext;
use Icicle\Coroutine;
use Icicle\Loop;
// Create a periodic message in the main thread.
$timer = Loop\periodic(1, function () {
print "Demonstrating how alive the parent is.\n";
});
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);
});
// 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();
$test->join()->then(function () use ($test) {
// Run the thread and wait asynchronously for it to finish.
$test->start();
yield $test->join();
print "Thread ended!\n";
})->done(function () {
Loop\stop();
});