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
广告