updates to pthreads API

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@328967 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Joe Watkins 2013-01-03 15:13:50 +00:00
parent 011885f7c8
commit 7d1dc9ad7d
35 changed files with 1080 additions and 15 deletions

View file

@ -8,8 +8,67 @@
<preface xml:id="intro.pthreads">
&reftitle.intro;
<para>
pthreads uses the well tested abilities of TSRM and PHP to provide object oreintated multi-threading capabilities.
pthreads is an Object Orientated API that allows user-land multi-threading in PHP.
It includes all the tools you need to create multi-threaded applications targeted at the Web or the Console.
PHP applications can create, read, write, execute and synchronize with Threads, Workers and Stackables.
</para>
<para>
A Thread Object:
The user can implement a thread by extending the Thread declaration provided by pthreads.
Any members can be written and read by any context with a reference to the Thread, any context can also execute any public and protected methods.
The run method of the implementation is executed in a separate thread when the start method of the implementation is called from the context ( that's Thread or Process ) that created it.
Only the context that creates a thread can start and join with it.
</para>
<para>
A Worker Object:
A Worker Thread has a persistent state, and will be available from the call to start until the object goes out of scope, or is explicitly shutdown.
Any context with a reference can pass objects of type Stackable to the Worker which will be executed by the Worker in a separate Thread.
The run method of a Worker is executed before any objects on the stack, such that it can initialize resources that the Stackables to come may need.
</para>
<para>
A Stackable Object:
A Stackable Object can read/write and execute the Worker ( $this->worker ) during the execution of it's run method.
Additionally, any context with a reference to the Stackable can read, write and execute it's methods before during and after execution.
</para>
<para>
Synchronization:
All of the objects that pthreads creates have built in synchronization in the form of ::wait and ::notify.
Calling ::wait on an object will cause the context to wait for another context to call ::notify on the same object.
This allows for powerful synchronization between Threaded Objects in PHP.
</para>
<para>
Wait, Threaded Objects ?
A Stackable, Thread or Worker can be thought of, and should be used as a Threaded stdClass: A Thread, Worker and Stackable all behave in the same way in any context with a reference.
Any objects that are intended for use in the multi-threaded parts of your application should extend the Stackable, Thread or Worker declaration.
Which means they must implement run but may not ever be executed; it will often be the case that Objects being used in a multi-threaded environment are intended for execution.
Doing so means any context ( that's Thread/Worker/Stackable/Process ) with a reference can read, write and execute the members of the Threaded Object before, during, and after execution.
</para>
<para>
Method Modifiers:
The protected methods of Threaded Objects are protected by pthreads, such that only one context may call that method at a time.
The private methods of Threaded Objects can only be called from within the Threaded Object during execution.
</para>
<para>
Data Storage:
As a rule of thumb, any data type that can be serialized can be used as a member of a Threaded object, it can be read and written from any context with a reference to the Threaded Object.
Not every type of data is stored serially, basic types are stored in their true form.
Complex types, Arrays, and Objects that are not Threaded are stored serially; they can be read and written to the Threaded Object from any context with a reference.
With the exception of Threaded Objects any reference used to set a member of a Threaded Object is separated from the reference in the Threaded Object;
the same data can be read directly from the Threaded Object at any time by any context with a reference to the Threaded Object.
</para>
<note>
<para>
Resources:
The extensions and functionality that define resources in PHP are completely unprepared for this kind of environment; pthreads makes provisions for Resources to be shared among contexts, however, for most types of resource it should be considered unsafe. Extreme caution and care should be used when sharing resources among contexts.
</para>
</note>
<caution>
<para>
pthreads was, and is, an experiment with pretty good results. Any of its limitations or features may change at any time; that is the nature of experimentation.
It's limitations - often imposed by the implementation - exist for good reason; the aim of pthreads is to provide a useable solution to multi-tasking in PHP at any level.
In the environment which pthreads executes, some restrictions and limitations are necessary in order to provide a stable environment.
</para>
</caution>
</preface>
&reference.pthreads.setup;

View file

@ -29,7 +29,7 @@
<varlistentry>
<term><parameter>condition</parameter></term>
<listitem>
<para>A handle to a Condition Variables returned by a previous call to <function>Cond::create</function></para>
<para>A handle to a Condition Variable returned by a previous call to <function>Cond::create</function></para>
</listitem>
</varlistentry>
</variablelist>
@ -42,7 +42,32 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Condition Broadcasting</title>
<programlisting role="php">
<![CDATA[
<?php
/** You cannot use the "new" keyword, a Cond is not a PHP object **/
$cond = Cond::create();
/** The caller must lock the associated Mutex before a call to broadcast **/
var_dump(Cond::broadcast($cond));
/** Always destroy Cond you have created **/
Cond::destroy($cond);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -29,6 +29,33 @@
&reftitle.returnvalues;
<para>A handle to a Condition Variable</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Condition Creation and Destruction</title>
<programlisting role="php">
<![CDATA[
<?php
/** You cannot use the "new" keyword, a Cond is not a PHP object **/
$cond = Cond::create();
/** You can now use the Cond in any context **/
var_dump($cond);
/** Always destroy Cond you have created **/
Cond::destroy($cond);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
int(4540682)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -47,7 +47,32 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Condition Creation and Destruction</title>
<programlisting role="php">
<![CDATA[
<?php
/** You cannot use the "new" keyword, a Cond is not a PHP object **/
$cond = Cond::create();
/** You can now use the Cond in any context **/
var_dump($cond);
/** Always destroy Cond you have created **/
Cond::destroy($cond);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
int(4540682)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -42,6 +42,32 @@
<para>A boolean indication of success.</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Condition Signalling</title>
<programlisting role="php">
<![CDATA[
<?php
/** You cannot use the "new" keyword, a Cond is not a PHP object **/
$cond = Cond::create();
/** The caller must lock the associated Mutex before a call to broadcast **/
var_dump(Cond::signal($cond));
/** Always destroy Cond you have created **/
Cond::destroy($cond);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>

View file

@ -60,6 +60,37 @@
<para>A boolean indication of success.</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Waiting for Conditions</title>
<programlisting role="php">
<![CDATA[
<?php
/** PLEASE NOTE THIS EXAMPLE WILL CAUSE THE PROCESS TO HANG **/
$mutex = Mutex::create(true);
/** You cannot use the "new" keyword, a Cond is not a PHP object **/
$cond = Cond::create();
/** The caller must lock the associated Mutex before a call to broadcast **/
var_dump(Cond::wait($mutex, $cond));
/** Always destroy Cond you have created **/
Cond::destroy($cond);
Mutex::unlock($mutex);
Mutex::destroy($mutex);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
int(49685473)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -39,6 +39,33 @@
&reftitle.returnvalues;
<para>A newly created and optionally locked Mutex handle</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Mutex Creation and Destruction</title>
<programlisting role="php">
<![CDATA[
<?php
/** You cannot use the "new" keyword, a Mutex is not a PHP object **/
$mutex = Mutex::create();
/** You have the physical address of the Mutex **/
var_dump($mutex);
/** Always destroy mutex you have created **/
Mutex::destroy($mutex);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
int(40096976)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -43,6 +43,37 @@
<para>A boolean indication of success</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>A newly created and optionally locked Mutex handle</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Mutex Creation and Destruction</title>
<programlisting role="php">
<![CDATA[
<?php
/** You cannot use the "new" keyword, a Mutex is not a PHP object **/
$mutex = Mutex::create();
/** You have the physical address of the Mutex **/
var_dump($mutex);
/** Always destroy mutex you have created **/
Mutex::destroy($mutex);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
int(40096976)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>

View file

@ -41,6 +41,40 @@
<para>A boolean indication of success.</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>A newly created and optionally locked Mutex handle</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Mutex Locking and Unlocking</title>
<programlisting role="php">
<![CDATA[
<?php
/** You cannot use the "new" keyword, a Mutex is not a PHP object **/
$mutex = Mutex::create();
/** You can now lock the mutex in any context **/
var_dump(Mutex::lock($mutex));
/** It is invalid to attempt to destroy a locked Mutex **/
var_dump(Mutex::unlock($mutex));
/** Always destroy mutex you have created **/
Mutex::destroy($mutex);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>

View file

@ -41,6 +41,36 @@
A boolean indication of success.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Mutex Locking and Unlocking</title>
<programlisting role="php">
<![CDATA[
<?php
/** You cannot use the "new" keyword, a Mutex is not a PHP object **/
$mutex = Mutex::create();
/** You can now try to lock the mutex in any context **/
var_dump(Mutex::trylock($mutex));
/** It is invalid to attempt to destroy a locked Mutex **/
var_dump(Mutex::unlock($mutex));
/** Always destroy mutex you have created **/
Mutex::destroy($mutex);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -54,6 +54,36 @@
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Mutex Locking and Unlocking</title>
<programlisting role="php">
<![CDATA[
<?php
/** You cannot use the "new" keyword, a Mutex is not a PHP object **/
$mutex = Mutex::create();
/** You can now lock the mutex in any context **/
var_dump(Mutex::lock($mutex));
/** It is invalid to attempt to destroy a locked Mutex **/
var_dump(Mutex::unlock($mutex));
/** Always destroy mutex you have created **/
Mutex::destroy($mutex);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -13,7 +13,12 @@
<section xml:id="pthreads.installation">
&reftitle.install;
&no.install;
<para>
To enable pthreads support, configure PHP with <option role="configure">--enable-maintainer-zts</option> and <option role="configure">--enable-pthreads</option>
</para>
<para>
Windows users can download prebuilt binaries from GitHub or from PECL Windows.
</para>
</section>
<section xml:id="pthreads.configuration">
@ -23,9 +28,7 @@
<section xml:id="pthreads.resources">
&reftitle.resources;
<para>
</para>
&no.resource;
</section>
</chapter>

View file

@ -30,7 +30,47 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Detect the state of the referenced Stackable</title>
<programlisting role="php">
<![CDATA[
<?php
class Work extends Stackable {
public function run() {
$this->synchronized(function($object){
$object->wait();
}, $this);
}
}
class My extends Worker {
public function run() {
/** ... **/
}
}
$my = new My();
$work = new Work();
$my->stack($work);
$my->start();
$work->synchronized(function($object){
$object->notify();
}, $work);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -30,7 +30,50 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Detect the state of the referenced Stackable</title>
<programlisting role="php">
<![CDATA[
<?php
class Work extends Stackable {
public function run() {
$this->synchronized(function($object){
$object->wait();
}, $this);
}
}
class My extends Worker {
public function run() {
/** ... **/
}
}
$my = new My();
$work = new Work();
$my->stack($work);
$my->start();
usleep(10000);
$work->synchronized(function($object){
var_dump($object->isWaiting());
$object->notify();
}, $work);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -39,6 +39,50 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Notifications and Waiting</title>
<programlisting role="php">
<![CDATA[
<?php
class Work extends Stackable {
public function run() {
$this->synchronized(function($object){
printf("%s Synchronized\n", __CLASS__);
$object->notify();
}, $this);
}
}
class My extends Worker {
public function run() {
/** ... **/
}
}
$my = new My();
$my->start();
$work = array(new Work());
$my->stack($work[0]);
/** send notification to the waiting thread **/
$work[0]->synchronized(function($object){
printf("Process Synchronized\n");
$object->wait();
}, $work[0]);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Process Synchronized
Work Synchronized
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>

View file

@ -30,6 +30,33 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Return the identity of the Thread or Process that created the referenced Thread</title>
<programlisting role="php">
<![CDATA[
<?php
class My extends Thread {
public function run() {
printf("%s created by Thread #%lu\n", __CLASS__, $this->getCreatorId());
}
}
$my = new My();
$my->start();
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
My created by Thread #123456778899
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>

View file

@ -29,6 +29,35 @@
A numeric identity
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Return the identity of the referenced Thread</title>
<programlisting role="php">
<![CDATA[
<?php
class My extends Thread {
public function run() {
printf("%s is Thread #%lu\n", __CLASS__, $this->getThreadId());
}
}
$my = new My();
$my->start();
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
My is Thread #123456778899
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -30,7 +30,39 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Detect the state of the referenced Thread</title>
<programlisting role="php">
<![CDATA[
<?php
class My extends Thread {
public function run() {
$this->synchronized(function($thread){
$thread->wait();
}, $this);
}
}
$my = new My();
$my->start();
var_dump($my->isJoined());
$my->synchronized(function($thread){
$thread->notify();
}, $my);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(false)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -35,7 +35,39 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Detect the state of the referenced Thread</title>
<programlisting role="php">
<![CDATA[
<?php
class My extends Thread {
public function run() {
$this->synchronized(function($thread){
$thread->wait();
}, $this);
}
}
$my = new My();
$my->start();
var_dump($my->isRunning());
$my->synchronized(function($thread){
$thread->notify();
}, $my);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -30,6 +30,35 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Detect the state of the referenced Thread</title>
<programlisting role="php">
<![CDATA[
<?php
class My extends Thread {
public function run() {
/* ... */
}
}
$my = new My();
$my->start();
var_dump($my->isStarted());
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>

View file

@ -30,7 +30,39 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Detect the state of the referenced Thread</title>
<programlisting role="php">
<![CDATA[
<?php
class My extends Thread {
public function run() {
$this->synchronized(function($thread){
$thread->wait();
}, $this);
}
}
$my = new My();
$my->start();
$my->synchronized(function($thread){
var_dump($thread->isWaiting());
$thread->notify();
}, $my);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -26,10 +26,40 @@
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
A boolean indication of success
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Join with the referenced Thread</title>
<programlisting role="php">
<![CDATA[
<?php
class My extends Thread {
public function run() {
/* ... */
}
}
$my = new My();
$my->start();
/* ... */
var_dump($my->join());
/* ... */
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>

View file

@ -30,7 +30,41 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Notifications and Waiting</title>
<programlisting role="php">
<![CDATA[
<?php
class My extends Thread {
public function run() {
/** cause this thread to wait **/
$this->synchronized(function($thread){
$thread->wait();
}, $this);
}
}
$my = new My();
$my->start();
/** send notification to the waiting thread **/
$my->synchronized(function($thread){
$thread->notify();
}, $my);
var_dump($my->join());
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -30,6 +30,33 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Starting Threads</title>
<programlisting role="php">
<![CDATA[
<?php
class My extends Thread {
public function run() {
/** ... **/
}
}
$my = new My();
var_dump($my->start());
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>

View file

@ -38,6 +38,42 @@
A boolean indication of success
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Notifications and Waiting</title>
<programlisting role="php">
<![CDATA[
<?php
class My extends Thread {
public function run() {
/** cause this thread to wait **/
$this->synchronized(function($thread){
$thread->wait();
}, $this);
}
}
$my = new My();
$my->start();
/** send notification to the waiting thread **/
$my->synchronized(function($thread){
$thread->notify();
}, $my);
var_dump($my->join());
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -17,6 +17,9 @@
<function name='thread::iswaiting' from='PECL pthreads &gt;= 0.34'/>
<function name='thread::getthreadid' from='PECL pthreads &gt;= 0.34'/>
<function name='thread::getcreatorid' from='PECL pthreads &gt;= 0.36'/>
<function name='thread::synchronized' from='PECL pthreads &gt;= 0.40'/>
<function name='thread::lock' from='PECL pthreads &gt;= 0.40'/>
<function name='thread::unlock' from='PECL pthreads &gt;= 0.40'/>
<function name='worker::start' from='PECL pthreads &gt;= 0.36'/>
<function name='worker::run' from='PECL pthreads &gt;= 0.36'/>
<function name='worker::shutdown' from='PECL pthreads &gt;= 0.37'/>
@ -32,6 +35,9 @@
<function name='stackable::notify' from='PECL pthreads &gt;= 0.36'/>
<function name='stackable::isrunning' from='PECL pthreads &gt;= 0.36'/>
<function name='stackable::iswaiting' from='PECL pthreads &gt;= 0.36'/>
<function name='stackable::synchronized' from='PECL pthreads &gt;= 0.40'/>
<function name='stackable::lock' from='PECL pthreads &gt;= 0.40'/>
<function name='stackable::unlock' from='PECL pthreads &gt;= 0.40'/>
<function name='mutex::create' from='PECL pthreads &gt;= 0.34'/>
<function name='mutex::lock' from='PECL pthreads &gt;= 0.34'/>
<function name='mutex::trylock' from='PECL pthreads &gt;= 0.34'/>

View file

@ -30,7 +30,33 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Return the identity of the Thread or Process that created the referenced Worker</title>
<programlisting role="php">
<![CDATA[
<?php
class My extends Thread {
public function run() {
printf("%s created by Thread #%lu\n", __CLASS__, $this->getCreatorId());
}
}
$my = new My();
$my->start();
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
My created by Thread #123456778899
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -30,7 +30,45 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Returns the number of objects currently waiting to be executed by the referenced Worker</title>
<programlisting role="php">
<![CDATA[
<?php
class Work extends Stackable {
/** ... **/
public function run(){
/** ... **/
}
}
class My extends Worker {
public function run(){
/** ... **/
}
}
$my = new My();
/** ... **/
$my->stack(new Work());
/** ... **/
printf("My worker has %d jobs remaining\n", $my->getStacked());
/** ... **/
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
My worker has 5 jobs remaining
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -30,6 +30,33 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Return the identity of the referenced Worker</title>
<programlisting role="php">
<![CDATA[
<?php
class My extends Worker {
public function run() {
printf("%s is Worker #%lu\n", __CLASS__, $this->getThreadId());
}
}
$my = new My();
$my->start();
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
My is Worker #123456778899
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>

View file

@ -31,6 +31,38 @@
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Detect the state of a Worker</title>
<programlisting role="php">
<![CDATA[
<?php
class My extends Worker {
public function run() {
/* ... */
}
}
$my = new My();
$my->start();
var_dump($my->isShutdown());
$my->shutdown();
var_dump($my->isShutdown());
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(false)
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -30,6 +30,31 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Detect the state of a Worker</title>
<programlisting role="php">
<![CDATA[
<?php
class My extends Worker {
public function run() {
/* ... */
}
}
$my = new My();
$my->start();
/* ... */
if ($my->isWorking()) {
/* ... the Worker is busy executing another object */
}
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
</refentry>

View file

@ -29,6 +29,37 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Shutdown the referenced Worker</title>
<programlisting role="php">
<![CDATA[
<?php
class My extends Worker {
public function run() {
/* ... */
}
}
$my = new My();
$my->start();
/* ... */
var_dump($my->shutdown());
/* ... */
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -39,7 +39,43 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Passing Stackables to Workers for execution in the Worker Thread</title>
<programlisting role="php">
<![CDATA[
<?php
class Work extends Stackable {
/** ... **/
public function run(){
/** ... **/
}
}
class My extends Worker {
public function run(){
/** ... **/
}
}
$my = new My();
/** ... **/
var_dump($my->stack(new Work()));
/** ... **/
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
int(1)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -30,7 +30,33 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Starting Workers</title>
<programlisting role="php">
<![CDATA[
<?php
class My extends Worker {
public function run() {
/** ... **/
}
}
$my = new My();
var_dump($my->start());
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -39,7 +39,42 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Removing Stackables from Workers</title>
<programlisting role="php">
<![CDATA[
<?php
class Work extends Stackable {
public function run() {
}
}
class My extends Worker {
public function run() {
/** ... **/
}
}
$my = new My();
$work = new Work();
var_dump($my->stack($work));
var_dump($my->unstack($work));
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
int(1)
int(0)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file