<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/refs.fileprocess.process.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'ja',
  ),
  'this' => 
  array (
    0 => 'book.pthreads.php',
    1 => 'pthreads',
    2 => 'pthreads',
  ),
  'up' => 
  array (
    0 => 'refs.fileprocess.process.php',
    1 => 'プロセス制御',
  ),
  'prev' => 
  array (
    0 => 'parallel-sync.invoke.php',
    1 => 'parallel\\Sync::__invoke',
  ),
  'next' => 
  array (
    0 => 'pthreads.setup.php',
    1 => 'インストール/設定',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ja',
    'path' => 'reference/pthreads/book.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/book.pthreads.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="book.pthreads" class="book">
 
 <h1 class="title">pthreads</h1>
 

 <div id="intro.pthreads" class="preface">
  <h1 class="title">はじめに</h1>
  <p class="simpara">
    pthreads はオブジェクト指向の API で、PHP でマルチスレッド処理を行うのに必要なすべてのツールを提供します。
    PHP アプリケーションで、Thread や Worker そして Threaded を作ったり読み書きしたり実行したりできるようになります。
  </p>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="simpara">
    この拡張モジュールはメンテナンスされておらず、終了していると考えられます。
   </p>
  </div>
  <div class="tip"><strong class="tip">ヒント</strong>
   <p class="simpara">
    <a href="book.parallel.php" class="link">parallel</a> を代わりに使うことも検討してください。
   </p>
  </div>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="simpara">
    pthreads 拡張モジュールは、Webサーバー環境では使えません。
    よって、PHP でのスレッド処理は CLIベースのアプリケーションに限られています。
   </p>
  </div>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="simpara">
    ptheads (v3) は PHP 7.2+ で使えます。
    これは ZTS モードが 7.0 と 7.1 では安全でないためです。
   </p>
  </div>
  <p class="simpara">
   <span class="classname"><a href="class.threaded.php" class="classname">Threaded</a></span> クラスが、pthreads が実行できる機能のもとになります。
   このクラスは同期のメソッドとプログラマにとって便利なインタフェイスをいくつか公開しています。
  </p>
  <p class="simpara">
   <span class="classname"><a href="class.thread.php" class="classname">Thread</a></span> クラスと使うと、
   それを継承し、<code class="literal">run</code> メソッドを実装するだけでスレッドを作れます。
   あらゆるメンバにスレッドの参照を使ってあらゆるコンテキストで読み書きができます。
   また、あらゆるコンテキストが public と protected なメソッドを実行できます。
   run メソッドは、この実装オブジェクトが作られたスレッドとは別の、
   <span class="methodname"><a href="thread.start.php" class="methodname">Thread::start()</a></span> が呼ばれたスレッドで実行されます。
   スレッドを作ったコンテキストだけが、スレッドの start や join を行えます。
  </p>
  <p class="simpara">
   <span class="classname"><a href="class.worker.php" class="classname">Worker</a></span> クラスは永続的な状態を保持し、
   <span class="methodname"><a href="thread.start.php" class="methodname">Thread::start()</a></span> (またはそれを継承したメソッド) を呼んだ時点から
   オブジェクトがスコープ外に消えるまで、
   あるいは明示的に(<span class="methodname"><a href="worker.shutdown.php" class="methodname">Worker::shutdown()</a></span>経由で) shutdown するまで利用できます。
   このオブジェクトを参照する任意のコンテキストから、
   タスクを(<span class="methodname"><a href="worker.stack.php" class="methodname">Worker::stack()</a></span> 経由で)ワーカーに積むことができ、
   これを、Worker が別スレッドで実行します。
   Worker の <code class="literal">run</code> メソッドは、あらゆるオブジェクトがスタックに積まれる前に実行されます。
   そのため、オブジェクトで必要となるリソースの初期化に使えます。
  </p>
  <p class="simpara">
   <span class="classname"><a href="class.pool.php" class="classname">Pool</a></span> クラスを使って、
   <span class="classname"><a href="class.threaded.php" class="classname">Threaded</a></span> オブジェクトを分散させるワーカークラスのグループを作ることができます。
   複数スレッドを扱う際に最も簡単かつ効率的な方法です。
  </p>
  <div class="caution"><strong class="caution">警告</strong>
   <p class="simpara">
    <span class="classname"><a href="class.pool.php" class="classname">Pool</a></span> クラスは <span class="classname"><a href="class.threaded.php" class="classname">Threaded</a></span> クラスを継承していません。
    そして、Pool ベースのオブジェクトは通常の PHP オブジェクトです。
    よって、そのインスタンスをコンテキスト間で共有してはいけません。
   </p>
  </div>
  <p class="simpara">
   <span class="classname"><a href="class.volatile.php" class="classname">Volatile</a></span> は pthreads v3 で登場した新しいクラスです。
   これは <span class="classname"><a href="class.threaded.php" class="classname">Threaded</a></span> クラスのプロパティを変更可能にするために使います
   (なぜなら、これらはデフォルトでは変更できないからです)
   PHP の配列を <span class="classname"><a href="class.threaded.php" class="classname">Threaded</a></span> コンテキストに保存する目的でも使えます。
  </p>
  <p class="simpara">
   同期はスレッドを扱う際に重要な機能です。
   pthreads が作る全てのオブジェクトには (Java プログラマーならおなじみの)
   <span class="methodname"><a href="threaded.wait.php" class="methodname">Threaded::wait()</a></span> と
   <span class="methodname"><a href="threaded.notify.php" class="methodname">Threaded::notify()</a></span> による同期処理が組み込まれています。
   あるオブジェクトの <span class="methodname"><a href="threaded.wait.php" class="methodname">Threaded::wait()</a></span> を呼ぶと、
   別のコンテキストから同じオブジェクトの <span class="methodname"><a href="threaded.notify.php" class="methodname">Threaded::notify()</a></span>
   が呼ばれるのを待つようになります。
   これを使えば、PHP 内のスレッド化されたオブジェクト (<span class="classname"><a href="class.threaded.php" class="classname">Threaded</a></span> Object)
   どうしで強力な同期処理ができるようになります。
  </p>
  <div class="caution"><strong class="caution">警告</strong>
  <p class="simpara">
   マルチスレッドで実行されるあらゆるオブジェクトは <span class="classname"><a href="class.threaded.php" class="classname">Threaded</a></span> を継承すべきです。
  </p>
  </div>
  <p class="simpara">
   データストレージ:
   目安として、シリアライズ可能なデータ型なら何でも、スレッド化されたオブジェクトのメンバーとして使えます。
   そのオブジェクトへの参照を持つあらゆるコンテキストから、メンバーの読み書きができます。
   すべてのデータ型がシリアライズされるわけではなく、基本型はそのままの形式で格納されます。
   それ以外の複雑な型や配列、スレッド化されていないオブジェクトは、シリアライズして格納されます。
   これらは、参照を持つ任意のコンテキストから、スレッド化されたオブジェクトへの読み書きができます。
   スレッド化されたオブジェクトを例外として、
   あるスレッド化されたオブジェクトのメンバーを設定するために使うあらゆる参照は、
   そのスレッド化されたオブジェクト内の参照とは区別されます。
   同じデータを、いつでもどのコンテキストからでも、
   スレッド化されたオブジェクトから直接読み込めます。
  </p>
  <p class="simpara">
   static メンバー:
   新しいコンテキスト (Thread あるいは Worker) を作るときには、一般的にそれらはコピーされます。
   しかし、リソースおよび内部状態を持つオブジェクトは、安全性を考慮して null 化されます。
   これを、一種のスレッドローカルストレージとして使えます。
   たとえば、データベースサーバーへの接続情報と接続そのものをstatic なメンバーとして持つクラスがあるとします。
   コンテキストを開始するときには接続情報だけがコピーされ、接続自体はコピーされません。
   新しいコンテキスト上ではそのコンテキストを作ったオブジェクトと同じ方法で接続を立ち上げることができ、
   その接続を同じ場所に格納しても元のコンテキストには何も影響を及ぼしません。
  </p>
  <div class="caution"><strong class="caution">警告</strong>
  <p class="simpara">
   print_r や var_dump などのオブジェクトのデバッグ用関数を実行するきには、
   再帰の制限が含まれません。
  </p>
  </div>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    リソース:
    PHP のリソースを定義している拡張モジュールは、この手の環境で扱うには不十分です。
    pthreads はリソースをコンテキスト間で共有するための対策を用意していますが、
    大半のリソース型は安全には扱えません。
    コンテキスト間でリソースを共有するときには、いくら注意してもしすぎることはありません。
   </span>
  </p></blockquote>
  <div class="caution"><strong class="caution">警告</strong>
   <p class="simpara">
    pthreads を実行する環境下では、安定した環境を提供するためにいくつかの制約と限界があります。
   </p>
  </div>
 </div>

 





 




 




 





 





 




 




 





<ul class="chunklist chunklist_book"><li><a href="pthreads.setup.php">インストール/設定</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="pthreads.requirements.php">要件</a></li><li><a href="pthreads.installation.php">インストール手順</a></li></ul></li><li><a href="pthreads.constants.php">定義済み定数</a></li><li><a href="class.threaded.php">Threaded</a> — The Threaded class<ul class="chunklist chunklist_book chunklist_children"><li><a href="threaded.chunk.php">Threaded::chunk</a> — Manipulation</li><li><a href="threaded.count.php">Threaded::count</a> — Manipulation</li><li><a href="threaded.extend.php">Threaded::extend</a> — Runtime Manipulation</li><li><a href="thread.isrunning.php">Threaded::isRunning</a> — State Detection</li><li><a href="threaded.isterminated.php">Threaded::isTerminated</a> — State Detection</li><li><a href="threaded.merge.php">Threaded::merge</a> — Manipulation</li><li><a href="threaded.notify.php">Threaded::notify</a> — Synchronization</li><li><a href="threaded.notifyone.php">Threaded::notifyOne</a> — Synchronization</li><li><a href="threaded.pop.php">Threaded::pop</a> — Manipulation</li><li><a href="threaded.run.php">Threaded::run</a> — Execution</li><li><a href="threaded.shift.php">Threaded::shift</a> — Manipulation</li><li><a href="threaded.synchronized.php">Threaded::synchronized</a> — Synchronization</li><li><a href="threaded.wait.php">Threaded::wait</a> — Synchronization</li></ul></li><li><a href="class.thread.php">Thread</a> — Thread クラス<ul class="chunklist chunklist_book chunklist_children"><li><a href="thread.getcreatorid.php">Thread::getCreatorId</a> — 識別</li><li><a href="thread.getcurrentthread.php">Thread::getCurrentThread</a> — Identification</li><li><a href="thread.getcurrentthreadid.php">Thread::getCurrentThreadId</a> — Identification</li><li><a href="thread.getthreadid.php">Thread::getThreadId</a> — 識別</li><li><a href="thread.isjoined.php">Thread::isJoined</a> — 状態を検出する</li><li><a href="thread.isstarted.php">Thread::isStarted</a> — 状態を検出する</li><li><a href="thread.join.php">Thread::join</a> — 同期処理</li><li><a href="thread.start.php">Thread::start</a> — 実行する</li></ul></li><li><a href="class.worker.php">Worker</a> — Worker クラス<ul class="chunklist chunklist_book chunklist_children"><li><a href="worker.collect.php">Worker::collect</a> — Collect references to completed tasks</li><li><a href="worker.getstacked.php">Worker::getStacked</a> — Gets the remaining stack size</li><li><a href="worker.isshutdown.php">Worker::isShutdown</a> — State Detection</li><li><a href="worker.shutdown.php">Worker::shutdown</a> — Shutdown the worker</li><li><a href="worker.stack.php">Worker::stack</a> — Stacking work</li><li><a href="worker.unstack.php">Worker::unstack</a> — Unstacking work</li></ul></li><li><a href="class.collectable.php">Collectable</a> — The Collectable interface<ul class="chunklist chunklist_book chunklist_children"><li><a href="collectable.isgarbage.php">Collectable::isGarbage</a> — Determine whether an object has been marked as garbage</li></ul></li><li><a href="class.pool.php">Pool</a> — The Pool class<ul class="chunklist chunklist_book chunklist_children"><li><a href="pool.collect.php">Pool::collect</a> — Collect references to completed tasks</li><li><a href="pool.construct.php">Pool::__construct</a> — Creates a new Pool of Workers</li><li><a href="pool.resize.php">Pool::resize</a> — Resize the Pool</li><li><a href="pool.shutdown.php">Pool::shutdown</a> — Shutdown all workers</li><li><a href="pool.submit.php">Pool::submit</a> — Submits an object for execution</li><li><a href="pool.submitTo.php">Pool::submitTo</a> — Submits a task to a specific worker for execution</li></ul></li><li><a href="class.volatile.php">Volatile</a> — The Volatile class</li></ul></div><?php manual_footer($setup); ?>