The Volatile class

(PECL pthreads >= 3.0.0)

Introduction

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.

Synopsis de la classe

classVolatileextendsThreadedimplementsCollectable, Traversable {
publicThreaded::chunk(int$size, bool$preserve): array
publicThreaded::merge(mixed$from, bool$overwrite = ?): bool
publicThreaded::synchronized(Closure$block, mixed...$args): mixed
publicThreaded::wait(int$timeout = ?): bool
}

Exemples

Exemple #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);

Résultat de l'exemple ci-dessus est similaire à :

 RuntimeException: Threaded members previously set to Threaded objects are immutable, cannot overwrite data in %s:%d 

Exemple #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);

Résultat de l'exemple ci-dessus est similaire à :

 object(stdClass)#3 (0) { } 
To Top