{"id":13398368,"url":"https://github.com/krakjoe/pthreads","last_synced_at":"2025-03-14T02:31:18.417Z","repository":{"id":46557706,"uuid":"5589723","full_name":"krakjoe/pthreads","owner":"krakjoe","description":"Threading for PHP - Share Nothing, Do Everything :)","archived":true,"fork":false,"pushed_at":"2019-11-09T18:39:09.000Z","size":2622,"stargazers_count":3467,"open_issues_count":54,"forks_count":503,"subscribers_count":210,"default_branch":"master","last_synced_at":"2024-11-24T20:09:04.881Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/krakjoe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-08-28T17:57:40.000Z","updated_at":"2024-11-22T17:59:36.000Z","dependencies_parsed_at":"2022-09-11T14:12:14.416Z","dependency_job_id":null,"html_url":"https://github.com/krakjoe/pthreads","commit_stats":null,"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakjoe%2Fpthreads","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakjoe%2Fpthreads/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakjoe%2Fpthreads/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakjoe%2Fpthreads/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krakjoe","download_url":"https://codeload.github.com/krakjoe/pthreads/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243511660,"owners_count":20302595,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T19:00:23.989Z","updated_at":"2025-03-14T02:31:17.952Z","avatar_url":"https://github.com/krakjoe.png","language":"C","funding_links":[],"categories":["C","异步"],"sub_categories":[],"readme":"# Threading for PHP - Share Nothing, Do Everything :)\n\n[![Build Status](https://travis-ci.org/krakjoe/pthreads.svg?branch=master)](https://travis-ci.org/krakjoe/pthreads)\n[![Build status](https://ci.appveyor.com/api/projects/status/5pyrhbrxcgenkpbk/branch/master?svg=true)](https://ci.appveyor.com/project/krakjoe/pthreads/branch/master)\n[![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/krakjoe/pthreads.svg)](http://isitmaintained.com/project/krakjoe/pthreads \"Average time to resolve an issue\")\n[![Percentage of issues still open](http://isitmaintained.com/badge/open/krakjoe/pthreads.svg)](http://isitmaintained.com/project/krakjoe/pthreads \"Percentage of issues still open\")\n[![Join the chat at https://gitter.im/krakjoe/pthreads](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/krakjoe/pthreads?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\nThis project provides multi-threading that is compatible with PHP based on Posix Threads.\n\n## Highlights\n\n* An easy to use, quick to learn OO Threading API for PHP 7.2+\n* Execute any and all predefined and user declared methods and functions, including closures.\n* Ready made synchronization included\n* A world of possibilities ...\n\n## Technical Features\n\n* High Level Threading\n* Synchronization\n* Worker Threads\n* Thread Pools\n* Complete Support for OO - ie. traits, interfaces, inheritance etc\n* Full read/write/execute support for Threaded objects\n\n## Requirements\n\n* PHP 7.2+\n* ZTS Enabled ( Thread Safety )\n* Posix Threads Implementation\n\nTesting has been carried out on x86, x64 and ARM, in general you just need a compiler and pthread.h\n\n## PHP7\n\nFor PHP7, pthreads has been almost completely rewritten to be more efficient, easier to use and more robust. I will give a brief changelog here:\n\nThe API for v3 has changed, the following things have been removed:\n\n * ```Mutex```, ```Cond```, and ```Stackable```\n * ```Threaded::lock``` and ```Threaded::unlock```\n * ```Threaded::isWaiting```\n * ```Threaded::from```\n * ```Thread::kill``` (there be dragons)\n * ```Thread::detach```\n * ```Worker::isWorking```\n * ```Threaded::getTerminationInfo``` (this was unsafe, a better, safe impl can be done in userland)\n * Special behaviour of ```protected``` and ```private``` methods on ```Threaded``` objects\n\nThe following things have significant changes:\n \n * The method by which ```Threaded``` objects are stored as member properties of other ```Threaded``` objects.\n * The structure used by a ```Worker``` for stack (```Collectable``` objects to execute inserted by ```Worker::stack```).\n * The ```Pool::collect``` mechanism was moved from ```Pool``` to ```Worker``` for a more robust ```Worker``` and simpler ```Pool``` inheritance.\n * The method by which iteration occurs on ```Threaded``` objects, such that it uses memory more efficiently.\n * ```Threaded::synchronized``` provides true synchronization (state and properties lock).\n * ```Worker``` objects no longer require that you retain a reference to ```Collectable``` objects on the stack.\n * Unified monitor (cond/mutex/state) for ```Threaded``` objects\n * ```Threaded``` members of ```Threaded``` objects are immutable\n * ```Volatile``` objects, exempt from immutability\n * ```array``` coerced to ```Volatile``` when set as member of ```Threaded```\n * ```Collectable``` converted to interface, to make ```extends Volatile implements Collectable``` possible.\n\nSome blog posts explaining these changes:\n\n * [A Letter from the Future](http://blog.krakjoe.ninja/2015/08/a-letter-from-future.html)\n * [Addendum to A Letter from the Future](http://blog.krakjoe.ninja/2015/09/addendum-letter-from-future.html)\n\n### Supported PHP Versions\n\npthreads v3 requires PHP7 or above. PHP5 needs to use pthreads v2 which can be found in the PHP5 branch.\n\nNote 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.\n\n##### Unix-based Building from Source\n\nBuilding pthreads from source is quite simple on Unix-based OSs. The instructions are as follows:\n * Clone this repository and checkout the release to use (or master for the latest updates)\n * `cd pthreads`\n * `phpize`\n * `./configure`\n * `make`\n * `make install` (may need sudo)\n * Update your php.ini file to load the `pthreads.so` file using the `extension` directive\n\n### Windows Support\n\nYes !! Windows support is offered thanks to the pthread-w32 library.\n\nReleases for Windows can be found: http://windows.php.net/downloads/pecl/releases/pthreads/\n\n##### Simple Windows Installation\n\n* Add `pthreadVC2.dll` (included with the Windows releases) to the same directory as `php.exe` eg. `C:\\xampp\\php`\n* Add `php_pthreads.dll` to PHP extension folder eg. `C:\\xampp\\php\\ext`\n\n### Mac OSX Support\n\nYes !! Users of Mac will be glad to hear that pthreads is now tested on OSX as part of the development process.\n\n### Hello World\n\nAs is customary in our line of work:\n\n```php\n\u003c?php\n$thread = new class extends Thread {\n\tpublic function run() {\n\t\techo \"Hello World\\n\";\n\t}\n};\n\n$thread-\u003estart() \u0026\u0026 $thread-\u003ejoin();\n?\u003e\n```\n\n### Are you serious ?\n\nAbsolutely, 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 :)\n\n### SAPI Support\n\npthreads 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.\n\nSo I'm promoting the advice to hard and fast fact: *you can't use pthreads safely and sensibly anywhere but CLI.*\n\nThanks for listening ;)\n\n### Documentation \n\nDocumentation 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.\n\nFurther insights and occasional announcements can be read at the http://pthreads.org site where pthreads is developed and tested in the real world.\n\nHere are some links to articles I have prepared for users: everybody should read them before they do anything else:\n\n - https://gist.github.com/krakjoe/6437782\n - https://gist.github.com/krakjoe/9384409\n\nIf you have had the time to put any cool demo's together and would like them showcased on pthreads.org please get in touch.\n\n### Polyfill\n\nIt's possible to write code that optionally takes advantage of parallelism where the environment has ```pthreads``` loaded.\n\nThis is made possible by [pthreads-polyfill](http://github.com/krakjoe/pthreads-polyfill) which can be found on [packagist](https://packagist.org/packages/krakjoe/pthreads-polyfill).\n\nHaving required the appropriate package in your composer.json, the following code is executable everywhere:\n\n```php\n\u003c?php\nrequire_once(\"vendor/autoload.php\");\n\nif (extension_loaded(\"pthreads\")) {\n\t    echo \"Using pthreads\\n\";\n} else  echo \"Using polyfill\\n\";\n\n$pool = new Pool(4);\n\n$pool-\u003esubmit(new class extends Threaded {\n        public function run() {\n                echo \"Hello World\\n\";\n        }\n});\n\nwhile ($pool-\u003ecollect()) continue;\n\n$pool-\u003eshutdown();\n?\u003e\n```\n\nSome guidance on getting started, and detail regarding how the polyfill came to exist can be found [here](http://blog.krakjoe.ninja/2015/09/what-polly-really-wants.html).\n\n### Feedback\n\nPlease submit issues, and send your feedback and suggestions as often as you have them.\n\n### Reporting Bugs\n\nIf you believe you have found a bug in pthreads, please open an issue: Include in your report *minimal, executable, reproducing code*.\n\nMinimal:     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.\n\nExecutable:  include all the information required to execute the example code, code snippets are not helpful.\n\nReproducing: 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.\n\n__It is impossible to help without reproducing code, bugs that are opened without reproducing code will be closed.__\n\nPlease include version and operating system information in your report.\n\n*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.*\n\nHave patience; I am one human being.\n\n### Developers\n\nThere 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrakjoe%2Fpthreads","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrakjoe%2Fpthreads","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrakjoe%2Fpthreads/lists"}