pthreads Volatile 클래스

(PECL pthreads >= 3.0.0)


소개

Volatile 클래스는 pthreads v3의 새로운 기능입니다. 그것의 도입은 Threaded 클래스의 Threaded 멤버의 새로운 불변성 의미론의 결과입니다. Volatile 클래스는 Threaded 멤버의 가변성을 가능하게 하며 Threaded 컨텍스트에서 PHP 배열을 저장하는 데에도 사용됩니다.


클래스 개요

                  
class Volatile extends Threaded implements Collectable, Traversable {
/* Inherited methods */
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
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::wait(int $timeout = ?): bool
}
                  
                

Examples

예제 #1 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) {
}