Multi-threading in PHP
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 Threaded objects.
A Threaded Object: A Threaded Object forms the basis of the functionality that allows pthreads to operate. It exposes synchronization methods and some useful interfaces for the programmer.
A Thread: The user can implement a Thread by extending the Thread declaration provided by pthreads implementing the run method. 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 stack objects onto 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 objects to come may need.
A Pool: A Pool of Worker threads can be used to distribute Threaded objects among Workers. The Pool class included implements this functionality and takes care of referencing in a sane manner. Introduced in v1.0.0, the Pool implementation is the easiest and most efficient way of using multiple threads.