Penggunaan fungsi MODIFERS pada PHP

Ket : Pada baris terbawah, umumnya juga terdapat Daftar Isi halaman ini

Show


Introduction

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.

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.

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.

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.

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.

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.

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.

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.

Note:

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.

Caution

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.



Installing/Configuring

Daftar Isi

  • Requirements
  • Installation
  • Runtime Configuration
  • Resource Types

Requirements

pthreads requires a build of PHP with ZTS enabled ( --enable-maintainer-zts or --enable-zts on Windows )



Installation

To enable pthreads support, configure PHP with --enable-maintainer-zts and --enable-pthreads

Windows users can download prebuilt binaries from GitHub or from PECL Windows.



Runtime Configuration

This extension has no configuration directives defined in php.ini.



Resource Types

This extension has no resource types defined.




The Thread class

(No version information available, might only be in SVN)

Introduction

An implementation of a Thread should extend this declaration, implementing the run method. When the start method of that object is called, the run method code will be executed in separate Thread.

Class synopsis

Thread {

final public boolean isJoined ( void )

final public boolean isRunning ( void )

final public boolean isStarted ( void )

final public boolean isWaiting ( void )

final public boolean join ( void )

final public boolean lock ( void )

final public boolean notify ( void )

abstract public void run ( void )

final public boolean start ( void )

final public boolean unlock ( void )

final public boolean wait ([ long $timeout ] )

}


Thread::getCreatorId

(PECL pthreads >= 0.36)

Thread::getCreatorIdIdentification

Description

final public long Thread::getCreatorId ( void )

Parameters

This function has no parameters.

Return Values

A numeric identity

Examples

Example #1 Return the identity of the Thread or Process that created the referenced Thread

<?php
class My extends Thread {
public function
run() {
printf("%s created by Thread #%lu\n", __CLASS__, $this->getCreatorId());
}
}
$my = New My();
$my->start();
?>

The above example will output:

My created by Thread #123456778899


Thread::getThreadId

(PECL pthreads >= 0.34)

Thread::getThreadIdIdentification

Description

final public long Thread::getThreadId ( void )

Parameters

This function has no parameters.

Return Values

A numeric identity

Examples

Example #1 Return the identity of the referenced Thread

<?php
class My extends Thread {
public function
run() {
printf("%s is Thread #%lu\n", __CLASS__, $this->getThreadId());
}
}
$my = New My();
$my->start();
?>

The above example will output:

My is Thread #123456778899


Thread::isJoined

(PECL pthreads >= 0.34)

Thread::isJoinedState Detection

Description

final public boolean Thread::isJoined ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of state

Examples

Example #1 Detect the state of the referenced Thread

<?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);
?>

The above example will output:



Thread::isRunning

(PECL pthreads >= 0.34)

Thread::isRunningState Detection

Description

final public boolean Thread::isRunning ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of state

Note:

A Thread is considered running while executing the run method

Examples

Example #1 Detect the state of the referenced Thread

<?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);
?>

The above example will output:



Thread::isStarted

(PECL pthreads >= 0.34)

Thread::isStartedState Detection

Description

final public boolean Thread::isStarted ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of state

Examples

Example #1 Detect the state of the referenced Thread

<?php
class My extends Thread {
public function
run() {
/* ... */
}
}
$my = New My();
$my->start();
var_dump($my->isStarted());
?>

The above example will output:



Thread::isWaiting

(PECL pthreads >= 0.34)

Thread::isWaitingState Detection

Description

final public boolean Thread::isWaiting ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of state

Examples

Example #1 Detect the state of the referenced Thread

<?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);
?>

The above example will output:



Thread::join

(PECL pthreads >= 0.34)

Thread::joinSynchronization

Description

final public boolean Thread::join ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of success

Examples

Example #1 Join with the referenced Thread

<?php
class My extends Thread {
public function
run() {
/* ... */
}
}
$my = New My();
$my->start();
/* ... */
var_dump($my->join());
/* ... */
?>

The above example will output:



Thread::lock

(PECL pthreads >= 0.40)

Thread::lockSynchronization

Description

final public boolean Thread::lock ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of success

Examples

Example #1 Locking Thread Storage

<?php
class My extends Thread {
public function
run() {
var_dump($this->lock());
/** nobody can read or write **/
var_dump($this->unlock());
/** reading / writing resumed for all other contexts */
}
}
$my = New My();
$my->start();
?>

The above example will output:



Thread::notify

(PECL pthreads >= 0.34)

Thread::notifySynchronization

Description

final public boolean Thread::notify ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of success

Examples

Example #1 Notifications and Waiting

<?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());
?>

The above example will output:



Thread::run

(PECL pthreads >= 0.34)

Thread::runExecution

Description

abstract public void Thread::run ( void )

Parameters

This function has no parameters.

Return Values

The methods return value, if used, will be ignored



Thread::start

(PECL pthreads >= 0.34)

Thread::startExecution

Description

final public boolean Thread::start ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of success

Examples

Example #1 Starting Threads

<?php
class My extends Thread {
public function
run() {
/** ... **/
}
}
$my = New My();
var_dump($my->start());
?>

The above example will output:



Thread::synchronized

(PECL pthreads >= 0.40)

Thread::synchronizedSynchronization

Description

final public mixed Thread::synchronized ( Closure $block [, mixed $... ] )

Parameters

block

The block of code to execute

...

Variable length list of arguments to use as function arguments to the block

Return Values

The return value from the block

Examples

Example #1 Synchronizing

<?php
class My extends Thread {
public function
run() {
$this->synchronized(function($thread){
$thread->wait();
},
$this);
}
}
$my = New My();
$my->start();
$my->synchronized(function($thread){
$thread->notify();
},
$my);
var_dump($my->join());
?>

The above example will output:



Thread::unlock

(PECL pthreads >= 0.40)

Thread::unlockSynchronization

Description

final public boolean Thread::unlock ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of success

Examples

Example #1 Locking Thread Storage

<?php
class My extends Thread {
public function
run() {
var_dump($this->lock());
/** nobody can read or write **/
var_dump($this->unlock());
/** reading / writing resumed for all other contexts */
}
}
$my = New My();
$my->start();
?>

The above example will output:



Thread::wait

(PECL pthreads >= 0.34)

Thread::waitSynchronization

Description

final public boolean Thread::wait ([ long $timeout ] )

Parameters

timeout

An optional timeout in millionths of a second

Return Values

A boolean indication of success

Examples

Example #1 Notifications and Waiting

<?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());
?>

The above example will output:


Daftar Isi

  • Thread::getCreatorId Identification
  • Thread::getThreadId Identification
  • Thread::isJoined State Detection
  • Thread::isRunning State Detection
  • Thread::isStarted State Detection
  • Thread::isWaiting State Detection
  • Thread::join Synchronization
  • Thread::lock Synchronization
  • Thread::notify Synchronization
  • Thread::run Execution
  • Thread::start Execution
  • Thread::synchronized Synchronization
  • Thread::unlock Synchronization
  • Thread::wait Synchronization


The Worker class

(No version information available, might only be in SVN)

Introduction

Worker Threads have a persistent context, as such should be used over Threads in most cases.

Class synopsis

Worker {

final public boolean isWorking ( void )

abstract public void run ( void )

final public boolean shutdown ( void )

final public boolean start ( void )

}


Worker::getCreatorId

(PECL pthreads >= 0.36)

Worker::getCreatorIdIdentification

Description

final public long Worker::getCreatorId ( void )

Parameters

This function has no parameters.

Return Values

A numeric identity

Examples

Example #1 Return the identity of the Thread or Process that created the referenced Worker

<?php
class My extends Thread {
public function
run() {
printf("%s created by Thread #%lu\n", __CLASS__, $this->getCreatorId());
}
}
$my = New My();
$my->start();
?>

The above example will output:

My created by Thread #123456778899


Worker::getStacked

(PECL pthreads >= 0.36)

Worker::getStackedStack Analysis

Description

final public int Worker::getStacked ( void )

Parameters

This function has no parameters.

Return Values

An integral value

Examples

Example #1 Returns the number of objects currently waiting to be executed by the referenced Worker

<?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());
/** ... **/
?>

The above example will output:

My worker has 5 jobs remaining


Worker::getThreadId

(PECL pthreads >= 0.36)

Worker::getThreadIdIdentification

Description

final public long Worker::getThreadId ( void )

Parameters

This function has no parameters.

Return Values

A numeric identity

Examples

Example #1 Return the identity of the referenced Worker

<?php
class My extends Worker {
public function
run() {
printf("%s is Worker #%lu\n", __CLASS__, $this->getThreadId());
}
}
$my = New My();
$my->start();
?>

The above example will output:

My is Worker #123456778899


Worker::isShutdown

(PECL pthreads >= 0.37)

Worker::isShutdownState Detection

Description

final public boolean Worker::isShutdown ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of state

Examples

Example #1 Detect the state of a Worker

<?php
class My extends Worker {
public function
run() {
/* ... */
}
}
$my = New My();
$my->start();
var_dump($my->isShutdown());
$my->shutdown();
var_dump($my->isShutdown());
?>

The above example will output:



Worker::isWorking

(PECL pthreads >= 0.37)

Worker::isWorkingState Detection

Description

final public boolean Worker::isWorking ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of state

Examples

Example #1 Detect the state of a Worker

<?php
class My extends Worker {
public function
run() {
/* ... */
}
}
$my = New My();
$my->start();
/* ... */
if ($my->isWorking()) {
/* ... the Worker is busy executing another object */
}
?>



Worker::run

(PECL pthreads >= 0.36)

Worker::runExecution

Description

abstract public void Worker::run ( void )

Parameters

This function has no parameters.

Return Values

The methods return value, if used, will be ignored



Worker::shutdown

(PECL pthreads >= 0.37)

Worker::shutdownSynchronization

Description

final public boolean Worker::shutdown ( void )

Parameters

This function has no parameters.

Return Values

Examples

Example #1 Shutdown the referenced Worker

<?php
class My extends Worker {
public function
run() {
/* ... */
}
}
$my = New My();
$my->start();
/* ... */
var_dump($my->shutdown());
/* ... */
?>

The above example will output:



Worker::stack

(PECL pthreads >= 0.36)

Worker::stackStacking

Description

final public int Worker::stack ( Stackable $work )

Parameters

work

An object of type Stackable to be executed by the referenced Worker

Return Values

The new length of the stack

Examples

Example #1 Passing Stackables to Workers for execution in the Worker Thread

<?php
class Work extends Stackable {
/** ... **/public function run(){
/** ... **/
}
}

class

My extends Worker {
public function
run(){
/** ... **/
}
}
$my = New My();
/** ... **/
var_dump($my->stack(New Work()));
/** ... **/
?>

The above example will output:



Worker::start

(PECL pthreads >= 0.36)

Worker::startExecution

Description

final public boolean Worker::start ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of success

Examples

Example #1 Starting Workers

<?php
class My extends Worker {
public function
run() {
/** ... **/
}
}
$my = New My();
var_dump($my->start());
?>

The above example will output:



Worker::unstack

(PECL pthreads >= 0.36)

Worker::unstackStacking

Description

final public int Worker::unstack ([ Stackable $work ] )

Parameters

work

An object of type Stackable

Return Values

The new length of the stack

Examples

Example #1 Removing Stackables from Workers

<?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));
?>

The above example will output:


Daftar Isi

  • Worker::getCreatorId Identification
  • Worker::getStacked Stack Analysis
  • Worker::getThreadId Identification
  • Worker::isShutdown State Detection
  • Worker::isWorking State Detection
  • Worker::run Execution
  • Worker::shutdown Synchronization
  • Worker::stack Stacking
  • Worker::start Execution
  • Worker::unstack Stacking


The Stackable class

(No version information available, might only be in SVN)

Introduction

Stackables are tasks that are executed by Worker threads. You can synchronize with, read, and write Stackable objects before, after and during their execution.

Class synopsis

Stackable {

final public boolean isRunning ( void )

final public boolean isWaiting ( void )

final public boolean lock ( void )

final public boolean notify ( void )

abstract public void run ( void )

final public boolean unlock ( void )

final public boolean wait ([ string $timeout ] )

}


Stackable::isRunning

(PECL pthreads >= 0.36)

Stackable::isRunningState Detection

Description

final public boolean Stackable::isRunning ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of state

Examples

Example #1 Detect the state of the referenced Stackable

<?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);
?>

The above example will output:



Stackable::isWaiting

(PECL pthreads >= 0.36)

Stackable::isWaitingState Detection

Description

final public boolean Stackable::isWaiting ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of state

Examples

Example #1 Detect the state of the referenced Stackable

<?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);
?>

The above example will output:



Stackable::lock

(PECL pthreads >= 0.40)

Stackable::lockSynchronization

Description

final public boolean Stackable::lock ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of success



Stackable::notify

(PECL pthreads >= 0.36)

Stackable::notifySynchronization

Description

final public boolean Stackable::notify ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of state



Stackable::run

(PECL pthreads >= 0.36)

Stackable::runExecution

Description

abstract public void Stackable::run ( void )

Parameters

This function has no parameters.

Return Values

The methods return value, if used, will be ignored



Stackable::synchronized

(PECL pthreads >= 0.40)

Stackable::synchronizedSynchronization

Description

final public mixed Stackable::synchronized ( Closure $block [, mixed $... ] )

Parameters

block

The block of code to execute

...

Variable length list of arguments to use as function arguments to the block

Return Values

The return value from the block

Examples

Example #1 Synchronizing

<?php
class My extends Stackable {
public function
run() {
$this->synchronized(function($object){
var_dump($object->isWaiting());
$object->notify();
},
$this);
}
}
/** create worker **/
$my = New My();
/** stack my **/
/** execute something here **/
$my->synchronized(function($object){
$object->wait();
},
$my);
?>

The above example will output:

bool(/** true or false **/)


Stackable::unlock

(PECL pthreads >= 0.40)

Stackable::unlockSynchronization

Description

final public boolean Stackable::unlock ( void )

Parameters

This function has no parameters.

Return Values

A boolean indication of success

Examples

Example #1 Locking Object Storage

<?php
class Work extends Stackable {
public function
run() {
var_dump($this->lock());
/** nobody can read or write **/
var_dump($this->unlock());
/** reading / writing resumed for all contexts */
}
}
class
My extends Worker {
public function
run(){
/** ... **/
}
}
$my = New My();
$work = array(New Work());
$my->stack($work[0]);
$my->start();
?>

The above example will output:



Stackable::wait

(PECL pthreads >= 0.36)

Stackable::waitSynchronization

Description

final public boolean Stackable::wait ([ string $timeout ] )

Parameters

timeout

An optional timeout in millionths of a second.

Return Values

A boolean indication of success

Examples

Example #1 Notifications and Waiting

<?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]);
?>

The above example will output:

Process SynchronizedWork Synchronized

Daftar Isi

  • Stackable::isRunning State Detection
  • Stackable::isWaiting State Detection
  • Stackable::lock Synchronization
  • Stackable::notify Synchronization
  • Stackable::run Execution
  • Stackable::synchronized Synchronization
  • Stackable::unlock Synchronization
  • Stackable::wait Synchronization


Method Modifiers

pthreads overrides the functionality of the protected and private method modifiers in order to provide functionality more suited to multi-threaded objects. pthreads applies this functionality to all Thread, Worker and Stackable objects from creation.

Example #1 protected method example: protected methods can only be executed by one Thread at a time.

<?php
class ExampleThread extends Thread {
public function
run() {
/* thread code */
if ($this->synchronized()) {

}
}

protected function

synchronized() {
/* synchronized method */
}
}
$thread = New ExampleThread();
if (
$thread->start()) {
$thread->synchronized();
}
?>

Example #2 private method example: private methods may only be executed by the Thread, Worker, or Stackable during execution

<?php
class ExampleThread extends Thread {
public function
run() {
/* thread code */
if ($this->insideonly()) {

}
}

private function

insideonly() {
/* private method */
}
}
$thread = New ExampleThread();
if (
$thread->start()) {
$thread->insideonly();
}
?>


The Mutex class

(No version information available, might only be in SVN)

Introduction

The static methods contained in the Mutex class provide direct access to Posix Mutex functionality.

Class synopsis

Mutex {

final public static long create ([ boolean $lock ] )

final public static boolean destroy ( long $mutex )

final public static boolean lock ( long $mutex )

final public static boolean trylock ( long $mutex )

final public static boolean unlock ( long $mutex [, boolean $destroy ] )

}


Mutex::create

(PECL pthreads >= 0.34)

Mutex::createCreate a Mutex

Description

final public static long Mutex::create ([ boolean $lock ] )

Parameters

lock

Setting lock to true will lock the Mutex for the caller before returning the handle

Return Values

A newly created and optionally locked Mutex handle

Examples

Example #1 Mutex Creation and Destruction

<?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);
?>

The above example will output:



Mutex::destroy

(PECL pthreads >= 0.34)

Mutex::destroyDestroy Mutex

Description

final public static boolean Mutex::destroy ( long $mutex )

Parameters

mutex

A handle returned by a previous call to Mutex::create(). The handle should not be locked by any Thread when Mutex::destroy() is called.

Return Values

A boolean indication of success

Return Values

A newly created and optionally locked Mutex handle

Examples

Example #1 Mutex Creation and Destruction

<?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);
?>

The above example will output:



Mutex::lock

(PECL pthreads >= 0.34)

Mutex::lockAcquire Mutex

Description

final public static boolean Mutex::lock ( long $mutex )

An attempt to lock a Mutex owned (locked) by another Thread will result in blocking.

Return Values

A boolean indication of success.

Return Values

A newly created and optionally locked Mutex handle

Examples

Example #1 Mutex Locking and Unlocking

<?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);
?>

The above example will output:



Mutex::trylock

(PECL pthreads >= 0.34)

Mutex::trylockAttempt to Acquire Mutex

Description

final public static boolean Mutex::trylock ( long $mutex )

Return Values

A boolean indication of success.

Examples

Example #1 Mutex Locking and Unlocking

<?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);
?>

The above example will output:



Mutex::unlock

(PECL pthreads >= 0.34)

Mutex::unlockRelease Mutex

Description

final public static boolean Mutex::unlock ( long $mutex [, boolean $destroy ] )

Parameters

mutex

A handle returned by a previous call to Mutex::create().

destroy

When true pthreads will destroy the Mutex after a successful unlock.

Return Values

A boolean indication of success.

Examples

Example #1 Mutex Locking and Unlocking

<?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);
?>

The above example will output:


Daftar Isi

  • Mutex::create Create a Mutex
  • Mutex::destroy Destroy Mutex
  • Mutex::lock Acquire Mutex
  • Mutex::trylock Attempt to Acquire Mutex
  • Mutex::unlock Release Mutex


The Cond class

(No version information available, might only be in SVN)

Introduction

The static methods contained in the Cond class provide direct access to Posix Condition Variables.

Class synopsis

Cond {

final public static boolean broadcast ( long $condition )

final public static long create ( void )

final public static boolean destroy ( long $condition )

final public static boolean signal ( long $condition )

final public static boolean wait ( long $condition , long $mutex [, long $timeout ] )

}


Cond::broadcast

(PECL pthreads >= 0.34)

Cond::broadcastBroadcast a Condition

Description

final public static boolean Cond::broadcast ( long $condition )

Parameters

condition

A handle to a Condition Variable returned by a previous call to Cond::create()

Return Values

A boolean indication of success.

Examples

Example #1 Condition Broadcasting

<?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);
?>

The above example will output:



Cond::create

(PECL pthreads >= 0.34)

Cond::createCreate a Condition

Description

final public static long Cond::create ( void )

Parameters

This function has no parameters.

Return Values

A handle to a Condition Variable

Examples

Example #1 Condition Creation and Destruction

<?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);
?>

The above example will output:



Cond::destroy

(PECL pthreads >= 0.34)

Cond::destroyDestroy a Condition

Description

final public static boolean Cond::destroy ( long $condition )

Parameters

condition

A handle to a Condition Variable returned by a previous call to Cond::create()

Return Values

A boolean indication of success.

Examples

Example #1 Condition Creation and Destruction

<?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);
?>

The above example will output:



Cond::signal

(PECL pthreads >= 0.34)

Cond::signalSignal a Condition

Description

final public static boolean Cond::signal ( long $condition )

Parameters

condition

A handle returned by a previous call to Cond::create()

Return Values

A boolean indication of success.

Examples

Example #1 Condition Signalling

<?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);
?>

The above example will output:



Cond::wait

(PECL pthreads >= 0.34)

Cond::waitWait for Condition

Description

final public static boolean Cond::wait ( long $condition , long $mutex [, long $timeout ] )

Parameters

condition

A handle returned by a previous call to Cond::create().

mutex

A handle returned by a previous call to Mutex::create() and owned (locked) by the caller.

timeout

An optional timeout, in microseconds ( millionths of a second ).

Return Values

A boolean indication of success.

Examples

Example #1 Waiting for Conditions

<?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);
?>

The above example will output:


Daftar Isi

  • Cond::broadcast Broadcast a Condition
  • Cond::create Create a Condition
  • Cond::destroy Destroy a Condition
  • Cond::signal Signal a Condition
  • Cond::wait Wait for Condition

  • Introduction
  • Installing/Configuring
    • Requirements
    • Installation
    • Runtime Configuration
    • Resource Types
  • Thread The Thread class
    • Thread::getCreatorId Identification
    • Thread::getThreadId Identification
    • Thread::isJoined State Detection
    • Thread::isRunning State Detection
    • Thread::isStarted State Detection
    • Thread::isWaiting State Detection
    • Thread::join Synchronization
    • Thread::lock Synchronization
    • Thread::notify Synchronization
    • Thread::run Execution
    • Thread::start Execution
    • Thread::synchronized Synchronization
    • Thread::unlock Synchronization
    • Thread::wait Synchronization
  • Worker The Worker class
    • Worker::getCreatorId Identification
    • Worker::getStacked Stack Analysis
    • Worker::getThreadId Identification
    • Worker::isShutdown State Detection
    • Worker::isWorking State Detection
    • Worker::run Execution
    • Worker::shutdown Synchronization
    • Worker::stack Stacking
    • Worker::start Execution
    • Worker::unstack Stacking
  • Stackable The Stackable class
    • Stackable::isRunning State Detection
    • Stackable::isWaiting State Detection
    • Stackable::lock Synchronization
    • Stackable::notify Synchronization
    • Stackable::run Execution
    • Stackable::synchronized Synchronization
    • Stackable::unlock Synchronization
    • Stackable::wait Synchronization
  • Modifiers Method Modifiers
  • Mutex The Mutex class
    • Mutex::create Create a Mutex
    • Mutex::destroy Destroy Mutex
    • Mutex::lock Acquire Mutex
    • Mutex::trylock Attempt to Acquire Mutex
    • Mutex::unlock Release Mutex
  • Cond The Cond class
    • Cond::broadcast Broadcast a Condition
    • Cond::create Create a Condition
    • Cond::destroy Destroy a Condition
    • Cond::signal Signal a Condition
    • Cond::wait Wait for Condition

Copyright © 1997 - 2013 by the PHP Documentation Group. All rights reserved. Legal Notices