View Discussion Table of Contents Improve Article Save Article View Discussion Improve Article Save Article Thread-safe: It is used to ensure that when the shared data structure which is manipulated by different threads are prevented from entering
the race condition. Thread-safety is recommended when the web server run multiple threads of execution simultaneously for different requests. In Thread Safety binary can work in a multi-threaded web server context. Thread Safety works by creating a local storage copy in each thread so that the data will not collide with another thread.
Non-thread-safe: It does not check the safety of the
threads which makes it faster to run but at the same time, it becomes more unstable and crashes very frequently. It refers to a single thread only builds. In non-thread safe version binaries widespread use in the case of interaction with a web server through the FastCGI protocol, by not utilizing multi-threading.
So it depends on the way that you want to use PHP. AFAIR running PHP with the fastCGI is
the preferable way. If you are unknown which version of PHP is installed in your system then there is an easy way to know that.
PHP can support multithreading for compute intensive workloads. This tutorial will recompile PHP from source with ZTS enabled in order to allow us to then add the pthreads extension for the PHP CLI (not for Apache or FPM). I will not dive into when not to use multithreading in PHP, but its fair to say that there are good reasons why it supported for the webserver, and hence the pre-built package that you install from the Ubuntu repositories. The majority of the content in the answer below came from an answer on Stack Overflow for how to install php7 (zts) + pthreads on Ubuntu 14.04. However I have tweaked it as necessary for Ubuntu 16.04 and without even trying to add FPM support. This tutorial will set up the latest version of PHP from the source code. This is not a release, and thus should not be used in production. This tutorial will form a foundation on which I will create production suitable versions in future, using releases of PHP. Related Posts
Requirements
Video RunthroghRecompile PHPDownload the necessary packages for compilation.
Remove any existing php7 and recreate php7 and other subdirectories
Go to your home directory. And download the PHP source code that we will compile PHP from.
Download the pthreads source to the extensions folder.
Build PHP.
Add PthreadsWe are going to manually compile and add pthreads to PHP, so make the phpize and php-config helper programs executable
Run phpize on pthreads.
Set configuration options for pthreads.
Build and install the extension
Remove any link to PHP if it already exists. If this was a server from scratch and you only ran the steps in this tutorial, this step should do nothing.
Link to your newly compiled PHP binary.
TestingYou can check which version of PHP you have installed by running:
Multithreading TestIn order to test that multithreading was now available in the PHP CLI, I tweaked a script from a tutorial on Sitepoint and put it below
As you can see below, when I run the script, I have 5 PHP threads, one from the main thread, and then the 4 workers in the pool that I created. ConclusionWe now have support for multithreading in PHP on Ubuntu 16.04. If you wish to learn more about how to make use of pthreads, I recommend reading "Parallel Programming with Pthreads in PHP – the Fundamentals" and the man pages of course. References
Last updated: 16th September 2021 How do I enable thread safety?Check the version of installed PHP Thread safe or Non Thread Safe: Open a phpinfo() and search for the line Thread safety for a thread-safe build you should find enable. How do I enable ZTS PHP?Installation. Add the package repository. sudo add-apt-repository ppa:ondrej/php-zts sudo apt-get -y update.. Install ZTS enabled PHP. ... . Check ZTS is enabled. ... . Install pThreads via Pecl. ... . Check pThreads is installed.. Do I need thread safe PHP?So what do I choose? If you choose to run PHP as a CGI binary, then you won't need thread safety, because the binary is invoked at each request. For multithreaded webservers, such as IIS5 and IIS6, you should use the threaded version of PHP. What is PHP ZTS?This is used to enable the Zend Thread Safety package in PHP which is used for pthreads. From the PHP manual(http://php.net/manual/en/pthreads.requirements.php) pthreads requires a build of PHP with ZTS (Zend Thread Safety) enabled ( --enable-maintainer-zts or --enable-zts on Windows ) |