Why ZTS
is disabled by default and we should compile PHP source to achieve Threading
ability?
Is there any limitation or side effect when we enable ZTS
? Is it unstable?

- 1,960
- 1
- 18
- 27
-
given Derick explanation, also it's name ZTS, I don't think the unstable portion of the question should be answered. – Adi Prasetyo May 20 '20 at 11:14
1 Answers
PHP originally started out as a CGI binary, and then as a module for Apache. Neither of these two ways required PHP to be a threaded run time as they would all handle a request in sequence.
When support for other web servers, most notably Microsoft's IIS through their ISAPI interface, was added, their processing model required PHP to be able to run as a threaded process. PHP added a "ZTS" (Zend Thread Safe) mode that does a fair bit of work to make sure that requests that run in parallel (threaded) don't step on each others toes. But this does come at a performance cost.
As most web server APIs don't require ZTS to be enabled, the default for PHP is for that to be off. If you were to build PHP for the ISAPI SAPI (Server Abstraction) layer, then the PHP build process automatically turns on ZTS mode.
Having said all that, the ZTS mode is not about allowing threading inside PHP scripts, but rather to allow for PHP itself to run in a threaded environment. If you are interested in running things in parallel from a PHP's script point of view, you need to resort to third party extensions such as Joe's parallel extension, or swoole.

- 35,169
- 5
- 76
- 99
-
2Thanks for your good answer. I have two more questions :D 1. If ZTS is JUST for an internal purpose, why all threading third-party extensions (such as `parallel`) need it? 2. Why I must compile PHP from source to enable ZTS? Why we can't install it via package managers? Currently, I using `phpbrew` to compile PHP from source, but it confuses my collaborators. – Mostafa Lavaei May 21 '20 at 01:57
-
11. Because the `parallel` extension means that two PHP scripts will run at the same time in the same runtime, just as it would do with ISAPI. 2. Some package managers do include the ZTS variant, such as Fedora (https://fedoraproject.org/wiki/Packaging:PHP#PHP_ZTS_extension) – Derick May 24 '20 at 13:39
-
It still doesn't make sense to me that parallel requires ZTS. As they explain in the philosophy section (https://www.php.net/manual/en/philosophy.parallel.php), stuff running on the other threads doesn't even really share memory, they only share things by copies of values, which is similar to just running things from a shell and passing parameters. Couldn't it just start another instance of php and pass some startup parameters such as the name of a function it needs to run? Or does running multiple instance of php cause php to run incorrectly unless it is compiled with ZTS? – still_dreaming_1 Jul 15 '22 at 21:30