PHP - Threaded::synchronized() 函式



Threaded::synchronized - 同步

語法

public mixed Threaded::synchronized( Closure $block [, mixed $... ] )

Threaded::synchronized() 函式可以在保留呼叫上下文引用的物件的同步鎖的同時執行程式碼塊。

Threaded::synchronized() 函式可以從程式碼塊返回值。

示例

<?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());
?>
php_function_reference.htm
廣告