note 116167 added to threaded.notify
| From: | cottton at i-stats dot net | Date: | Tue, 18 Nov 2014 01:34:33 +0000 |
| Subject: | note 116167 added to threaded.notify | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200941@lists.php.net to get a copy of this message | ||
Seems like some operators dont work.
f.e. $thread->array[] = 1; fails
a simple test:
<?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();
}
/** cause this thread to wait **/
$this->synchronized(
function($thread){
if(count($this->array) < 1){
$thread->wait();
}
},
$this
);
echo PHP_EOL;
if($this->stop){
break;
}
} // while
}
}
$my = new My();
$my->start();
sleep(1); // wait a bit
// test 1 - $thread->array[] = 1;
$my->synchronized(
function($thread){
$thread->msg = 'test 1';
$thread->array[] = 1;
$thread->notify();
},
$my
);
sleep(1); // wait a bit
// test 2 - array_push($thread->array, 2);
$my->synchronized(
function($thread){
$thread->msg = 'test 2';
array_push($thread->array, 2);
$thread->notify();
},
$my
);
sleep(1); // wait a bit
// test 3 - array_push($thread->array, 2);
$my->synchronized(
function($thread){
$thread->msg = 'test 3';
$new = array(3);
$thread->array = array_merge($thread->array, $new);
$thread->notify();
},
$my
);
sleep(1); // wait a bit
$my->stop = true;
?>
out:
default
string(13) "default val 1"
string(13) "default val 2"
test 1
test 2
test 3
int(3)
so in this case only array_merge() worked.
----
Server IP: 72.52.91.14
Probable Submitter: 178.25.108.124
----
Manual Page -- http://php.net/manual/en/threaded.notify.php
Edit -- https://master.php.net/note/edit/116167
Del: integrated -- https://master.php.net/note/delete/116167/integrated
Del: useless -- https://master.php.net/note/delete/116167/useless
Del: bad code -- https://master.php.net/note/delete/116167/bad+code
Del: spam -- https://master.php.net/note/delete/116167/spam
Del: non-english -- https://master.php.net/note/delete/116167/non-english
Del: in docs -- https://master.php.net/note/delete/116167/in+docs
Del: other reasons-- https://master.php.net/note/delete/116167
Reject -- https://master.php.net/note/reject/116167
Search -- https://master.php.net/manage/user-notes.php