Threaded::notify

(PECL pthreads >= 2.0.0)

Threaded::notify β€” Бинхронизация

ОписаниС

public function Threaded::notify(): bool

ΠžΡ‚ΠΏΡ€Π°Π²Π»ΡΠ΅Ρ‚ ΡƒΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΠ΅ ΡƒΠΊΠ°Π·Π°Π½Π½ΠΎΠΌΡƒ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Ρƒ.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

Π‘ΠΈΠ³Π½Π°Ρ‚ΡƒΡ€Π° Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Π½Π΅ содСрТит ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ².

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true, Ссли Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΠ»Π°ΡΡŒ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ, ΠΈΠ»ΠΈ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 УвСдомлСния ΠΈ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅

<?php
class My extends Thread {
public function
run() {
/** Π·Π°ΡΡ‚Π°Π²ΠΈΡ‚ΡŒ этот ΠΏΠΎΡ‚ΠΎΠΊ ΠΆΠ΄Π°Ρ‚ΡŒ **/
$this->synchronized(function($thread){
if (!
$thread->done)
$thread->wait();
},
$this);
}
}
$my = new My();
$my->start();
/** ΠΎΡ‚ΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ ΡƒΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΠ΅ ΠΎΠΆΠΈΠ΄Π°ΡŽΡ‰Π΅ΠΌΡƒ ΠΏΠΎΡ‚ΠΎΠΊΡƒ **/
$my->synchronized(function($thread){
$thread->done = true;
$thread->notify();
},
$my);
var_dump($my->join());
?>

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°:

bool(true)
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 1 note

up
-4
cottton at i-stats dot net ΒΆ
11 years ago
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.