PHP Conference Japan 2024

Threaded::wait

(PECL pthreads >= 2.0.0)

Threaded::wait同步

說明

public Threaded::wait(int $timeout = ?): bool

將使呼叫上下文等待來自參考物件的通知

參數

timeout

以微秒為單位的選用逾時時間

回傳值

成功時回傳 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)

新增註記

使用者貢獻的註記

此頁面沒有使用者貢獻的註記。
To Top