« Pool::submitTo
セマフォ »

The Volatile class

(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.

クラス概要

class Volatile extends Threaded implements Collectable, Traversable {
/* 継承したメソッド */
public function Threaded::chunk(int $size, bool $preserve): array
public function Threaded::count(): int
public function Threaded::extend(string $class): bool
public function Threaded::isRunning(): bool
public function Threaded::isTerminated(): bool
public function Threaded::merge(mixed $from, bool $overwrite = ?): bool
public function Threaded::notify(): bool
public function Threaded::notifyOne(): bool
public function Threaded::pop(): bool
public function Threaded::run(): void
public function Threaded::shift(): mixed
public function Threaded::synchronized(Closure $block, mixed ...$args): mixed
public function Threaded::wait(int $timeout = ?): bool
}

例

例1 New immutability semantics of Threaded

<?php

class 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

<?php

class 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) {
}
関連キーワード:
Threaded   Volatile   mixed   Task   pthreads   members   extends   stdClass   immutability   semantics   construct   property   dump   Pool   submitTo   セマフォ   consequence   classes   store   arrays   contexts   implements   Collectable   Traversable   継承
関連する関数・ページ:
threaded.count   threaded.shift   threaded.pop   threaded.extend   threaded.merge   threaded.run

他のマニュアルの関連ページ