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 のスタックに置くと、そのオブジェクトの 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 )プロパティ
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 上の例の出力は、 たとえば以下のようになります。 RuntimeException: Threaded members previously set to Threaded objects are immutable, cannot overwrite data in %s:%d 例2 Volatile use-case
<?php 上の例の出力は、 たとえば以下のようになります。 object(stdClass)#3 (0) { } |