array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'class.parallel-future.php', 1 => 'parallel\\Future', ), 'up' => array ( 0 => 'book.parallel.php', 1 => 'parallel', ), 'prev' => array ( 0 => 'parallel-runtime.kill.php', 1 => 'parallel\\Runtime::kill', ), 'next' => array ( 0 => 'parallel-future.cancel.php', 1 => 'parallel\\Future::cancel', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/parallel/parallel.future.xml', ), 'extra_header_links' => array ( 'rel' => 'alternate', 'href' => '/manual/en/feeds/class.parallel-future.atom', 'type' => 'application/atom+xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(0.8.0)
A Future represents the return value or uncaught exception from a task, and exposes an API for cancellation.
示例 #1 Example showing Future as return value
<?php
$runtime = new \parallel\Runtime;
$future = $runtime->run(function(){
return "World";
});
printf("Hello %s\n", $future->value());
?>
以上示例的输出类似于:
Hello World
The behaviour of a future also allows it to be used as a simple synchronization point even where the task does not return a value explicitly.
示例 #2 Example showing Future as synchronization point
<?php
$runtime = new \parallel\Runtime;
$future = $runtime->run(function(){
echo "in child ";
for ($i = 0; $i < 500; $i++) {
if ($i % 10 == 0) {
echo ".";
}
}
echo " leaving child";
});
$future->value();
echo "\nparent continues\n";
?>
以上示例的输出类似于:
in child .................................................. leaving child parent continues