Ket : Pada baris terbawah, umumnya juga terdapat Daftar Isi halaman ini Show
Introductionpthreads 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.
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/ConfiguringDaftar Isi
Requirementspthreads requires a build of PHP with ZTS enabled ( --enable-maintainer-zts or --enable-zts on Windows ) InstallationTo 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 ConfigurationThis extension has no configuration directives defined in php.ini. Resource TypesThis extension has no resource types defined. The Thread class(No version information available, might only be in SVN) IntroductionAn 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 synopsisThread { 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 } Thread::getCreatorId(PECL pthreads >= 0.36) Thread::getCreatorId — Identification Descriptionfinal public long Thread::getCreatorId ( void ) ParametersThis function has no parameters. Return ValuesA numeric identity ExamplesExample #1 Return the identity of the Thread or Process that created the referenced Thread
The above example will output: My created by Thread #123456778899 Thread::getThreadId(PECL pthreads >= 0.34) Thread::getThreadId — Identification Descriptionfinal public long Thread::getThreadId ( void ) ParametersThis function has no parameters. Return ValuesA numeric identity ExamplesExample #1 Return the identity of the referenced Thread
The above example will output: My is Thread #123456778899 Thread::isJoined(PECL pthreads >= 0.34) Thread::isJoined — State Detection Descriptionfinal public boolean Thread::isJoined ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of state ExamplesExample #1 Detect the state of the referenced Thread
The above example will output: Thread::isRunning(PECL pthreads >= 0.34) Thread::isRunning — State Detection Descriptionfinal public boolean Thread::isRunning ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of state
ExamplesExample #1 Detect the state of the referenced Thread
The above example will output: Thread::isStarted(PECL pthreads >= 0.34) Thread::isStarted — State Detection Descriptionfinal public boolean Thread::isStarted ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of state ExamplesExample #1 Detect the state of the referenced Thread
The above example will output: Thread::isWaiting(PECL pthreads >= 0.34) Thread::isWaiting — State Detection Descriptionfinal public boolean Thread::isWaiting ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of state ExamplesExample #1 Detect the state of the referenced Thread
The above example will output: Thread::join(PECL pthreads >= 0.34) Thread::join — Synchronization Descriptionfinal public boolean Thread::join ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of success ExamplesExample #1 Join with the referenced Thread
The above example will output: Thread::lock(PECL pthreads >= 0.40) Thread::lock — Synchronization Descriptionfinal public boolean Thread::lock ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of success ExamplesExample #1 Locking Thread Storage
The above example will output: Thread::notify(PECL pthreads >= 0.34) Thread::notify — Synchronization Descriptionfinal public boolean Thread::notify ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of success ExamplesExample #1 Notifications and Waiting
The above example will output: Thread::run(PECL pthreads >= 0.34) Thread::run — Execution Descriptionabstract public void Thread::run ( void ) ParametersThis function has no parameters. Return ValuesThe methods return value, if used, will be ignored Thread::start(PECL pthreads >= 0.34) Thread::start — Execution Descriptionfinal public boolean Thread::start ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of success ExamplesExample #1 Starting Threads
The above example will output: Thread::synchronized(PECL pthreads >= 0.40) Thread::synchronized — Synchronization Descriptionfinal public mixed Thread::synchronized ( Closure Parametersblock The block of code to execute ... Variable length list of arguments to use as function arguments to the block Return ValuesThe return value from the block ExamplesExample #1 Synchronizing
The above example will output: Thread::unlock(PECL pthreads >= 0.40) Thread::unlock — Synchronization Descriptionfinal public boolean Thread::unlock ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of success ExamplesExample #1 Locking Thread Storage
The above example will output: Thread::wait(PECL pthreads >= 0.34) Thread::wait — Synchronization Descriptionfinal public boolean Thread::wait ([ long Parameterstimeout An optional timeout in millionths of a second Return ValuesA boolean indication of success ExamplesExample #1 Notifications and Waiting
The above example will output: Daftar Isi
The Worker class(No version information available, might only be in SVN) IntroductionWorker Threads have a persistent context, as such should be used over Threads in most cases. Class synopsisWorker { 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::getCreatorId — Identification Descriptionfinal public long Worker::getCreatorId ( void ) ParametersThis function has no parameters. Return ValuesA numeric identity ExamplesExample #1 Return the identity of the Thread or Process that created the referenced Worker
The above example will output: My created by Thread #123456778899 Worker::getStacked(PECL pthreads >= 0.36) Worker::getStacked — Stack Analysis Descriptionfinal public int Worker::getStacked ( void ) ParametersThis function has no parameters. Return ValuesAn integral value ExamplesExample #1 Returns the number of objects currently waiting to be executed by the referenced Worker
My extends Worker { The above example will output: My worker has 5 jobs remaining Worker::getThreadId(PECL pthreads >= 0.36) Worker::getThreadId — Identification Descriptionfinal public long Worker::getThreadId ( void ) ParametersThis function has no parameters. Return ValuesA numeric identity ExamplesExample #1 Return the identity of the referenced Worker
The above example will output: My is Worker #123456778899 Worker::isShutdown(PECL pthreads >= 0.37) Worker::isShutdown — State Detection Descriptionfinal public boolean Worker::isShutdown ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of state ExamplesExample #1 Detect the state of a Worker
The above example will output: Worker::isWorking(PECL pthreads >= 0.37) Worker::isWorking — State Detection Descriptionfinal public boolean Worker::isWorking ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of state ExamplesExample #1 Detect the state of a Worker
Worker::run(PECL pthreads >= 0.36) Worker::run — Execution Descriptionabstract public void Worker::run ( void ) ParametersThis function has no parameters. Return ValuesThe methods return value, if used, will be ignored Worker::shutdown(PECL pthreads >= 0.37) Worker::shutdown — Synchronization Descriptionfinal public boolean Worker::shutdown ( void ) ParametersThis function has no parameters. Return ValuesExamplesExample #1 Shutdown the referenced Worker
The above example will output: Worker::stack(PECL pthreads >= 0.36) Worker::stack — Stacking Descriptionfinal public int Worker::stack ( Stackable Parameterswork An object of type Stackable to be executed by the referenced Worker Return ValuesThe new length of the stack ExamplesExample #1 Passing Stackables to Workers for execution in the Worker Thread
My extends Worker { The above example will output: Worker::start(PECL pthreads >= 0.36) Worker::start — Execution Descriptionfinal public boolean Worker::start ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of success ExamplesExample #1 Starting Workers
The above example will output: Worker::unstack(PECL pthreads >= 0.36) Worker::unstack — Stacking Descriptionfinal public int Worker::unstack ([ Stackable Parameterswork An object of type Stackable Return ValuesThe new length of the stack ExamplesExample #1 Removing Stackables from Workers
My extends Worker { The above example will output: Daftar Isi
The Stackable class(No version information available, might only be in SVN) IntroductionStackables are tasks that are executed by Worker threads. You can synchronize with, read, and write Stackable objects before, after and during their execution. Class synopsisStackable { 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 } Stackable::isRunning(PECL pthreads >= 0.36) Stackable::isRunning — State Detection Descriptionfinal public boolean Stackable::isRunning ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of state ExamplesExample #1 Detect the state of the referenced Stackable
My extends Worker { The above example will output: Stackable::isWaiting(PECL pthreads >= 0.36) Stackable::isWaiting — State Detection Descriptionfinal public boolean Stackable::isWaiting ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of state ExamplesExample #1 Detect the state of the referenced Stackable
My extends Worker { The above example will output: Stackable::lock(PECL pthreads >= 0.40) Stackable::lock — Synchronization Descriptionfinal public boolean Stackable::lock ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of success Stackable::notify(PECL pthreads >= 0.36) Stackable::notify — Synchronization Descriptionfinal public boolean Stackable::notify ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of state Stackable::run(PECL pthreads >= 0.36) Stackable::run — Execution Descriptionabstract public void Stackable::run ( void ) ParametersThis function has no parameters. Return ValuesThe methods return value, if used, will be ignored Stackable::synchronized(PECL pthreads >= 0.40) Stackable::synchronized — Synchronization Descriptionfinal public mixed Stackable::synchronized ( Closure Parametersblock The block of code to execute ... Variable length list of arguments to use as function arguments to the block Return ValuesThe return value from the block ExamplesExample #1 Synchronizing
The above example will output: bool(/** true or false **/) Stackable::unlock(PECL pthreads >= 0.40) Stackable::unlock — Synchronization Descriptionfinal public boolean Stackable::unlock ( void ) ParametersThis function has no parameters. Return ValuesA boolean indication of success ExamplesExample #1 Locking Object Storage
The above example will output: Stackable::wait(PECL pthreads >= 0.36) Stackable::wait — Synchronization Descriptionfinal public boolean Stackable::wait ([ string Parameterstimeout An optional timeout in millionths of a second. Return ValuesA boolean indication of success ExamplesExample #1 Notifications and Waiting
The above example will output: Process SynchronizedWork Synchronized Daftar Isi
Method Modifierspthreads 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.
synchronized() { Example #2 private method example: private methods may only be executed by the Thread, Worker, or Stackable during execution
insideonly() { The Mutex class(No version information available, might only be in SVN) IntroductionThe static methods contained in the Mutex class provide direct access to Posix Mutex functionality. Class synopsisMutex { final
public static long create ([ boolean final public static boolean destroy ( long final public static boolean lock ( long final public
static boolean trylock ( long final public static boolean unlock ( long } Mutex::create(PECL pthreads >= 0.34) Mutex::create — Create a Mutex Descriptionfinal public static long Mutex::create ([ boolean Parameterslock Setting lock to true will lock the Mutex for the caller before returning the handle Return ValuesA newly created and optionally locked Mutex handle ExamplesExample #1 Mutex Creation and Destruction
The above example will output: Mutex::destroy(PECL pthreads >= 0.34) Mutex::destroy — Destroy Mutex Descriptionfinal public static boolean Mutex::destroy ( long Parametersmutex 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 ValuesA boolean indication of success Return ValuesA newly created and optionally locked Mutex handle ExamplesExample #1 Mutex Creation and Destruction
The above example will output: Mutex::lock(PECL pthreads >= 0.34) Mutex::lock — Acquire Mutex Descriptionfinal public static boolean Mutex::lock ( long An attempt to lock a Mutex owned (locked) by another Thread will result in blocking. Return ValuesA boolean indication of success. Return ValuesA newly created and optionally locked Mutex handle ExamplesExample #1 Mutex Locking and Unlocking
The above example will output: Mutex::trylock(PECL pthreads >= 0.34) Mutex::trylock — Attempt to Acquire Mutex Descriptionfinal public static boolean Mutex::trylock ( long Return ValuesA boolean indication of success. ExamplesExample #1 Mutex Locking and Unlocking
The above example will output: Mutex::unlock(PECL pthreads >= 0.34) Mutex::unlock — Release Mutex Descriptionfinal public static boolean Mutex::unlock ( long Parametersmutex A handle returned by a previous call to Mutex::create(). destroy When true pthreads will destroy the Mutex after a successful unlock. Return ValuesA boolean indication of success. ExamplesExample #1 Mutex Locking and Unlocking
The above example will output: Daftar Isi
The Cond class(No version information available, might only be in SVN) IntroductionThe static methods contained in the Cond class provide direct access to Posix Condition Variables. Class synopsisCond { final public static boolean broadcast ( long final public static long create ( void ) final public static boolean destroy ( long final public static boolean signal ( long final public static
boolean wait ( long } Cond::broadcast(PECL pthreads >= 0.34) Cond::broadcast — Broadcast a Condition Descriptionfinal public static boolean
Cond::broadcast ( long Parameterscondition A handle to a Condition Variable returned by a previous call to Cond::create() Return ValuesA boolean indication of success. ExamplesExample #1 Condition Broadcasting
The above example will output: Cond::create(PECL pthreads >= 0.34) Cond::create — Create a Condition Descriptionfinal public static long Cond::create ( void ) ParametersThis function has no parameters. Return ValuesA handle to a Condition Variable ExamplesExample #1 Condition Creation and Destruction
The above example will output: Cond::destroy(PECL pthreads >= 0.34) Cond::destroy — Destroy a Condition Descriptionfinal public static boolean Cond::destroy ( long Parameterscondition A handle to a Condition Variable returned by a previous call to Cond::create() Return ValuesA boolean indication of success. ExamplesExample #1 Condition Creation and Destruction
The above example will output: Cond::signal(PECL pthreads >= 0.34) Cond::signal — Signal a Condition Descriptionfinal
public static boolean Cond::signal ( long Parameterscondition A handle returned by a previous call to Cond::create() Return ValuesA boolean indication of success. ExamplesExample #1 Condition Signalling
The above example will output: Cond::wait(PECL pthreads >= 0.34) Cond::wait — Wait for Condition Descriptionfinal public static boolean Cond::wait ( long Parameterscondition 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 ValuesA boolean indication of success. ExamplesExample #1 Waiting for Conditions
The above example will output: Daftar Isi
Copyright © 1997 - 2013 by the PHP Documentation Group. All rights reserved. Legal Notices |