函数名:parallel\Sync::set()
适用版本:PHP 7.2.0 及以上版本
用法:parallel\Sync::set() 方法用于设置并行计算中共享变量的值。
语法:
public static void parallel\Sync::set(string $name, mixed $value)
参数:
返回值:无返回值。
示例:
<?php
$sharedVariable = new parallel\Sync();
// 在主线程中设置共享变量的值
$sharedVariable->set('count', 10);
// 在子线程中读取共享变量的值
$childThread = new parallel\Runtime();
$childThread->run(function() use ($sharedVariable) {
$count = $sharedVariable->get('count');
echo "Child thread: count = $count" . PHP_EOL;
});
// 在主线程中修改共享变量的值
$sharedVariable->set('count', 20);
// 在主线程中读取共享变量的值
$count = $sharedVariable->get('count');
echo "Main thread: count = $count" . PHP_EOL;
?>
输出:
Child thread: count = 10
Main thread: count = 20
说明: