pthreads

目次

The Threaded class

はじめに

Threaded objects form the basis of pthreads ability to execute user code in parallel; they expose synchronization methods and various useful interfaces.

Threaded objects, most importantly, provide implicit safety for the programmer; all operations on the object scope are safe.

クラス概要

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

Thread クラス

はじめに

このオブジェクトの start メソッドが呼ばれると、run メソッドのコードが個別のスレッドで並列処理されます。

run メソッドの実行後は Thread はすぐに終了し、作成元のスレッドに適切な時期に join します。

警告

Thread をいつ join させるのかをエンジンに決めさせていると、予期せぬ振る舞いを引き起こすことがあります。 可能な限り、プログラマーが明示的に指定するようにしましょう。

クラス概要

Thread
class Thread extends Threaded implements Countable, Traversable, ArrayAccess {
/* メソッド */
public int getCreatorId()
public static Thread getCurrentThread()
public static int getCurrentThreadId()
public int getThreadId()
public bool isJoined()
public bool isStarted()
public bool join()
public bool start(int $options = ?)
/* 継承したメソッド */
public array Threaded::chunk(int $size, bool $preserve)
public int Threaded::count()
public bool Threaded::extend(string $class)
public bool Threaded::isRunning()
public bool Threaded::isTerminated()
public bool Threaded::merge(mixed $from, bool $overwrite = ?)
public bool Threaded::notify()
public bool Threaded::notifyOne()
public bool Threaded::pop()
public void Threaded::run()
public mixed Threaded::shift()
public mixed Threaded::synchronized(Closure $block, mixed ...$args)
public bool Threaded::wait(int $timeout = ?)
}

Worker クラス

はじめに

ワーカースレッドには永続コンテキストがあり、たいていの場合はスレッドに対して使えます。

ワーカーを開始させると run メソッドを実行しますが、以下のいずれかの条件を満たすまでスレッドは終了しません。

  • Worker がスコープから外れる (どこからも参照されなくなる)

  • プログラマーが shutdown を呼ぶ

  • スクリプトが終了する

つまり、プログラマーは実行中のコンテキストを再利用できるということです。 オブジェクトを Worker のスタックに置くと、そのオブジェクトの run メソッドを Worker が実行します。

クラス概要

Worker
class Worker extends Thread implements Traversable, Countable, ArrayAccess {
/* メソッド */
public int collect(Callable $collector = ?)
public int getStacked()
public bool isShutdown()
public bool shutdown()
public int stack(Threaded &$work)
public int unstack()
/* 継承したメソッド */
public int Thread::getCreatorId()
public static Thread Thread::getCurrentThread()
public static int Thread::getCurrentThreadId()
public int Thread::getThreadId()
public bool Thread::isJoined()
public bool Thread::isStarted()
public bool Thread::join()
public bool Thread::start(int $options = ?)
}

The Collectable interface

はじめに

Represents a garbage-collectable object.

インターフェイス概要

Collectable
interface Collectable {
/* メソッド */
public true isGarbage()
}

The Pool class

はじめに

A Pool is a container for, and controller of, an adjustable number of Workers.

Pooling provides a higher level abstraction of the Worker functionality, including the management of references in the way required by pthreads.

クラス概要

Pool
class Pool {
/* プロパティ */
protected $size;
protected $class;
protected $workers;
protected $ctor;
protected $last;
/* メソッド */
public __construct(int $size, string $class = ?, array $ctor = ?)
public int collect(Callable $collector = ?)
public void resize(int $size)
public void shutdown()
public int submit(Threaded $task)
public int submitTo(int $worker, Threaded $task)
}

プロパティ

size

maximum number of Workers this Pool can use

class

the class of the Worker

workers

references to Workers

ctor

the arguments for constructor of new Workers

last

offset in workers of the last Worker used

The Volatile class

はじめに

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

例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) {
}