(PECL pthreads >= 3.0.0)
はじめに
The Volatile class is new to pthreads v3. Its introduction is a consequence of the new immutability semantics of Threaded members of Threaded classes. The Volatile class enables for mutability of its Threaded members, and is also used to store PHP arrays in Threaded contexts.
クラス概要
Volatile extends Threaded implements Collectable , Traversable {
public Threaded::chunk
( int $size
, bool $preserve
) : array
public Threaded::count ( ) : int
public Threaded::extend
( string $class
) : bool
public Threaded::from
( Closure $run
[, Closure $construct
[, array $args
]] ) : Threaded
public Threaded::getTerminationInfo ( ) : array
public Threaded::isRunning ( ) : bool
public Threaded::isTerminated ( ) : bool
public Threaded::isWaiting ( ) : bool
public Threaded::lock ( ) : bool
public Threaded::merge
( mixed $from
[, bool $overwrite
] ) : bool
public Threaded::notify ( ) : bool
public Threaded::notifyOne ( ) : bool
public Threaded::pop ( ) : bool
public Threaded::run ( ) : void
public Threaded::shift ( ) : mixed
public Threaded::synchronized
( Closure $block
, mixed ...$args
) : mixed
public Threaded::unlock ( ) : bool
public Threaded::wait
([ int $timeout
] ) : bool
}
例
例1 New immutability semantics of Threaded
<?phpclass Task extends Threaded{ public function __construct() { $this->data = new Threaded(); // attempt to overwrite a Threaded property of a Threaded class (invalid) $this->data = new StdClass(); }}var_dump((new Task())->data);
上の例の出力は、 たとえば以下のようになります。
RuntimeException: Threaded members previously set to Threaded objects are immutable, cannot overwrite data in %s:%d
例2 Volatile use-case
<?phpclass Task extends Volatile{ public function __construct() { $this->data = new Threaded(); // attempt to overwrite a Threaded property of a Volatile class (valid) $this->data = new StdClass(); }}var_dump((new Task())->data);
上の例の出力は、 たとえば以下のようになります。
object(stdClass)#3 (0) { }
/* 継承したメソッド */