The Volatile class

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 Threaded::chunk(int $size, bool $preserve): array
public Threaded::count(): int
public Threaded::extend(string $class): bool
public Threaded::isRunning(): bool
public Threaded::isTerminated(): bool
publicThreaded::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(): boolean
public Threaded::synchronized(Closure $block, mixed $... = ?): mixed
public 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) {
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *