似乎有些运算符不起作用。
例如,$thread->array[] = 1; 失败
一个简单的测试
<?php
class My extends Thread
{
public
$array = array('default val 1', 'default val 2'),
$msg = 'default',
$stop = false;
public function run()
{
while(true)
{
echo $this->msg . PHP_EOL;
if(count($this->array) > 0){
foreach($this->array as $val){
var_dump($val);
}
$this->array = array();
}
$this->synchronized(
function($thread){
if(count($this->array) < 1){
$thread->wait();
}
},
$this
);
echo PHP_EOL;
if($this->stop){
break;
}
} }
}
$my = new My();
$my->start();
sleep(1); $my->synchronized(
function($thread){
$thread->msg = 'test 1';
$thread->array[] = 1;
$thread->notify();
},
$my
);
sleep(1); $my->synchronized(
function($thread){
$thread->msg = 'test 2';
array_push($thread->array, 2);
$thread->notify();
},
$my
);
sleep(1); $my->synchronized(
function($thread){
$thread->msg = 'test 3';
$new = array(3);
$thread->array = array_merge($thread->array, $new);
$thread->notify();
},
$my
);
sleep(1); $my->stop = true;
?>
out
default
string(13) "default val 1"
string(13) "default val 2"
test 1
test 2
test 3
int(3)
所以在这个例子中只有 array_merge() 工作了。