• This repository has been archived on 17/Dec/2019
  • Stars
    star
    3,471
  • Rank 12,840 (Top 0.3 %)
  • Language
    C
  • License
    Other
  • Created over 12 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Threading for PHP - Share Nothing, Do Everything :)

Threading for PHP - Share Nothing, Do Everything :)

Build Status Build status Average time to resolve an issue Percentage of issues still open Join the chat at https://gitter.im/krakjoe/pthreads

This project provides multi-threading that is compatible with PHP based on Posix Threads.

Highlights

  • An easy to use, quick to learn OO Threading API for PHP 7.2+
  • Execute any and all predefined and user declared methods and functions, including closures.
  • Ready made synchronization included
  • A world of possibilities ...

Technical Features

  • High Level Threading
  • Synchronization
  • Worker Threads
  • Thread Pools
  • Complete Support for OO - ie. traits, interfaces, inheritance etc
  • Full read/write/execute support for Threaded objects

Requirements

  • PHP 7.2+
  • 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

PHP7

For PHP7, pthreads has been almost completely rewritten to be more efficient, easier to use and more robust. I will give a brief changelog here:

The API for v3 has changed, the following things have been removed:

  • Mutex, Cond, and Stackable
  • Threaded::lock and Threaded::unlock
  • Threaded::isWaiting
  • Threaded::from
  • Thread::kill (there be dragons)
  • Thread::detach
  • Worker::isWorking
  • Threaded::getTerminationInfo (this was unsafe, a better, safe impl can be done in userland)
  • Special behaviour of protected and private methods on Threaded objects

The following things have significant changes:

  • The method by which Threaded objects are stored as member properties of other Threaded objects.
  • The structure used by a Worker for stack (Collectable objects to execute inserted by Worker::stack).
  • The Pool::collect mechanism was moved from Pool to Worker for a more robust Worker and simpler Pool inheritance.
  • The method by which iteration occurs on Threaded objects, such that it uses memory more efficiently.
  • Threaded::synchronized provides true synchronization (state and properties lock).
  • Worker objects no longer require that you retain a reference to Collectable objects on the stack.
  • Unified monitor (cond/mutex/state) for Threaded objects
  • Threaded members of Threaded objects are immutable
  • Volatile objects, exempt from immutability
  • array coerced to Volatile when set as member of Threaded
  • Collectable converted to interface, to make extends Volatile implements Collectable possible.

Some blog posts explaining these changes:

Supported PHP Versions

pthreads v3 requires PHP7 or above. PHP5 needs to use pthreads v2 which can be found in the PHP5 branch.

Note that only PHP 7.2+ is now supported (requiring the current master branch of pthreads). This is due to safety issues with ZTS mode on PHP 7.0 and 7.1.

Unix-based Building from Source

Building pthreads from source is quite simple on Unix-based OSs. The instructions are as follows:

  • Clone this repository and checkout the release to use (or master for the latest updates)
  • cd pthreads
  • phpize
  • ./configure
  • make
  • make install (may need sudo)
  • Update your php.ini file to load the pthreads.so file using the extension directive

Windows Support

Yes !! Windows support is offered thanks to the pthread-w32 library.

Releases for Windows can be found: http://windows.php.net/downloads/pecl/releases/pthreads/

Simple Windows Installation
  • Add pthreadVC2.dll (included with the Windows releases) to the same directory as php.exe eg. C:\xampp\php
  • Add php_pthreads.dll to PHP extension folder eg. C:\xampp\php\ext

Mac OSX Support

Yes !! Users of Mac will be glad to hear that pthreads is now tested on OSX as part of the development process.

Hello World

As is customary in our line of work:

<?php
$thread = new class extends Thread {
	public function run() {
		echo "Hello World\n";
	}
};

$thread->start() && $thread->join();
?>

Are you serious ?

Absolutely, this is not a hack, we don't use forking or any other such nonsense, what you create are honest to goodness posix threads that are completely compatible with PHP and safe ... this is true multi-threading :)

SAPI Support

pthreads v3 is restricted to operating in CLI only: I have spent many years trying to explain that threads in a web server just don't make sense, after 1,111 commits to pthreads I have realised that, my advice is going unheeded.

So I'm promoting the advice to hard and fast fact: you can't use pthreads safely and sensibly anywhere but CLI.

Thanks for listening ;)

Documentation

Documentation can be found in the PHP manual: http://docs.php.net/manual/en/book.pthreads.php, and some examples can be found in the "examples" folder in the master repository.

Further insights and occasional announcements can be read at the http://pthreads.org site where pthreads is developed and tested in the real world.

Here are some links to articles I have prepared for users: everybody should read them before they do anything else:

If you have had the time to put any cool demo's together and would like them showcased on pthreads.org please get in touch.

Polyfill

It's possible to write code that optionally takes advantage of parallelism where the environment has pthreads loaded.

This is made possible by pthreads-polyfill which can be found on packagist.

Having required the appropriate package in your composer.json, the following code is executable everywhere:

<?php
require_once("vendor/autoload.php");

if (extension_loaded("pthreads")) {
	    echo "Using pthreads\n";
} else  echo "Using polyfill\n";

$pool = new Pool(4);

$pool->submit(new class extends Threaded {
        public function run() {
                echo "Hello World\n";
        }
});

while ($pool->collect()) continue;

$pool->shutdown();
?>

Some guidance on getting started, and detail regarding how the polyfill came to exist can be found here.

Feedback

Please submit issues, and send your feedback and suggestions as often as you have them.

Reporting Bugs

If you believe you have found a bug in pthreads, please open an issue: Include in your report minimal, executable, reproducing code.

Minimal: reduce your problem to the smallest amount of code possible; This helps with hunting the bug, but also it helps with integration and regression testing once the bug is fixed.

Executable: include all the information required to execute the example code, code snippets are not helpful.

Reproducing: some bugs don't show themselves on every execution, that's fine, mention that in the report and give an idea of how often you encounter the bug.

It is impossible to help without reproducing code, bugs that are opened without reproducing code will be closed.

Please include version and operating system information in your report.

Please do not post requests to help with code on github; I spend a lot of time on Stackoverflow, a much better place for asking questions.

Have patience; I am one human being.

Developers

There is no defined API for you to create your own threads in your extensions, this project aims to provide Userland threading, it does not aim to provide a threading API for extension developers. I suggest you allow users to decide what they thread and keep your own extension focused on your functionality.

More Repositories

1

parallel

A succinct parallel concurrency API for PHP8
C
1,448
star
2

apcu

APCu - APC User Cache
C
964
star
3

phpdbg

The Interactive PHP Debugger
C
837
star
4

pcov

PCOV - CodeCoverage compatible driver for PHP
C
695
star
5

ui

Cross platform UI development in PHP
C
516
star
6

tombs

Detect unused code in production
C
428
star
7

uopz

User Operations for Zend
C
354
star
8

stat

A super modern high performance profiler for production
C
209
star
9

jitfu

Creating native instructions in PHP since 2014
C
191
star
10

explain

Explain
JavaScript
110
star
11

pthreads-polyfill

A polyfill for pthreads
PHP
80
star
12

ilimit

Limit time and memory consumption of individual calls
C
70
star
13

idbg

Inspector Debugger
PHP
70
star
14

wkhtmltox

Converting HTML to X since 2017
C
66
star
15

promises

Promises in PHP
PHP
64
star
16

ustring

UnicodeString for PHP7
C++
64
star
17

autostrict

Automatic strict types in PHP7
C
64
star
18

inspector

Disassembler and Debug Kit for PHP 7
C
55
star
19

sandbox

A sandbox environment for PHP7.1+
C
54
star
20

cmark

CommonMark for PHP
C
46
star
21

mimus

mocking framework as light as a bird ...
PHP
46
star
22

pcov-clobber

Run PCOV in versions of PHPUnit before 8, if you must ...
PHP
40
star
23

componere

Complex Type Composition and Manipulation
C
39
star
24

apcu-bc

APCu Backwards Compatiblity Module
C
33
star
25

SIMD

Single Instruction, Multiple Data
C
30
star
26

pthreads-autoloading-composer

An example of how to use composers autoloader in conjunction with Pools
PHP
29
star
27

memoize

Caching the result of your expensive function calls since 2016
C
23
star
28

profiler

An extension to profile PHP
C
19
star
29

php-jansson

jannson based json encoder and decoder for PHP
C
18
star
30

trace

Tracing for PHP7 Processes
C
18
star
31

snappy

Snappy Compression for PHP
PHP
15
star
32

indexed

Educational Extension for PHP7
C
13
star
33

router

This extension serves to provide a sane, easy router for modern PHP applications:
C
13
star
34

uref

weak refs, for the brave ...
C++
13
star
35

phpdbg-ui

phpdbg remote console client
Java
13
star
36

ponion

onion server for php
C
10
star
37

operators

Override operators in userland
C
9
star
38

HandlerSocket

HandlerSocket PHP 7 Extension
C
9
star
39

apcup

APCu Pooling
C
9
star
40

utypes

User verified types
C
9
star
41

overload

Overloading Zend (RESEARCH)
C
9
star
42

bundle

An experimental method of bundling PHP code with extensions
C
7
star
43

nocheq

Sick of pesky type checking making your code too slow (and correct) ?
C
7
star
44

kore

kore php functions
C
6
star
45

perf

Sampling Profiler for PHP 7 (Unfinished)
C
6
star
46

Object

Multiple Inheritance at Runtime
C
5
star
47

cmark-visitors

Visitors for CommonMark implementing some useful AST transformations
PHP
4
star
48

instrumental

Componere Instrumental
PHP
4
star
49

u2fh

PHP7 u2fh
C
4
star
50

u2fs

PHP7 u2fs
C
3
star
51

sysconf

Get configuration information at run time
C
3
star
52

jitfi

PHP FFI header for libjit
C
3
star
53

cloname

Demonstrate how to give closures long names
C
2
star
54

dec64

dec64
Assembly
2
star
55

transformer

C
2
star
56

zi

Zend Instruments
C
2
star
57

ohash

spl_object_hash alternative playground
C
1
star
58

ui-doc

Documentation Resources for UI
PHP
1
star
59

apcu-ps

APCu PS Module
C
1
star