1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-27 12:54:55 +01:00
parallel/examples/thread.php

29 lines
738 B
PHP
Raw Normal View History

2015-08-07 01:59:25 +02:00
#!/usr/bin/env php
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;
2015-08-07 01:59:25 +02:00
$timer = Loop\periodic(1, function () {
print "Demonstrating how alive the parent is.\n";
});
Coroutine\create(function () {
// Create a periodic message in the main thread.
2015-07-14 00:30:59 +02:00
// Create a new child thread that does some blocking stuff.
2015-08-07 01:59:25 +02:00
$test = new ThreadContext(function () {
print "Sleeping for 5 seconds...\n";
sleep(5);
return 42;
});
2015-07-27 00:53:00 +02:00
// Run the thread and wait asynchronously for it to finish.
$test->start();
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();