Home  >  Article  >  Backend Development  >  PHP thread library: pthreads

PHP thread library: pthreads

WBOY
WBOYOriginal
2016-07-25 09:12:301225browse

This project provides a POSIX-based multi-threaded programming mechanism for PHP. Any defined user-defined methods and functions can be executed asynchronously. Built-in support for sync and sync methods.

Highlights

  • An easy-to-use, quick-to-learn Threading API for PHP5.3+
  • Execute any and all predefined and user declared methods and functions asynchronously
  • Ready made synchronization included, geared towards the PHP environment
  • Seamless operation in multi-threaded SAPI environments
  • A world of possibilities...

Technical Features

  • Posix Threads
  • Synchronization
  • Worker Threads
  • Synchronized Methods
  • Complete Support for OO - ie. traits, interfaces, inheritance
  • Full read/write/execute support for threaded objects
  • Mutex (direct, subset)
  • Conditions (direct, subset)

pthreads was written with simplicity, compatibility and efficiency in mind, it's performance beggars belief !!

Environmental requirements

  • PHP5.3+
  • ZTS Enabled (Thread Safety)
  • Posix Threads Implementation

Testing has been carried out on x86, x64 and ARM, in general you just need a compiler and pthread.h

PHP version supported

pthreads should compile and work in any version of PHP from 5.3.0 to the latest release.

Sample code:

  1. class AsyncOperation extends Thread {
  2. public function __construct($arg){
  3. $this->arg = $arg;
  4. }
  5. public function run(){
  6. if($this->arg){
  7. printf("Hello %sn", $this->arg);
  8. }
  9. }
  10. }
  11. $thread = new AsyncOperation("World");
  12. if($thread ->start())
  13. $thread->join();
  14. ?>
Copy code

Project homepage:http://www.open-open.com/lib/view/ home/1391824675848



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn