Threaded::notify
(PECL pthreads >= 2.0.0)
Threaded::notify — Synchronization
Description
public boolean Threaded::notify
( void
)
Send notification to the referenced object
Parameters
This function has no parameters.
Return Values
A boolean indication of success
Examples
Example #1 Notifications and Waiting
<?php
class My extends Thread {
public function run() {
/** cause this thread to wait **/
$this->synchronized(function($thread){
if (!$thread->done)
$thread->wait();
}, $this);
}
}
$my = new My();
$my->start();
/** send notification to the waiting thread **/
$my->synchronized(function($thread){
$thread->done = true;
$thread->notify();
}, $my);
var_dump($my->join());
?>
The above example will output:
bool(true)
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для управления процессами программ
- pthreads
- Функция Threaded::chunk() - Manipulation
- Функция Threaded::count() - Manipulation
- Threaded::extend
- Threaded::from
- Функция Threaded::getTerminationInfo() - Error Detection
- Функция Threaded::isRunning() - State Detection
- Функция Threaded::isTerminated() - State Detection
- Функция Threaded::isWaiting() - State Detection
- Функция Threaded::lock() - Synchronization
- Функция Threaded::merge() - Manipulation
- Функция Threaded::notify() - Synchronization
- Функция Threaded::pop() - Manipulation
- Функция Threaded::run() - Execution
- Функция Threaded::shift() - Manipulation
- Функция Threaded::synchronized() - Synchronization
- Функция Threaded::unlock() - Synchronization
- Функция Threaded::wait() - Synchronization
Коментарии
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.