- What is thread safe or non-thread safe in PHP?
- 4 Answers 4
- Needed background on concurrency approaches:
- Needed background on how PHP «integrates» with web servers:
- Now, onto your question!
- Final notes
- What is thread safe or non-thread safe in PHP ?
- PHP-What is thread safe or non-thread safe in PHP
- Non-Thread Safe:
- Uses of thread safe and non-thread safe:
- What does thread safety mean when downloading PHP
- Difference between PHP thread safe and non thread safe binaries
- PHP Thread Safe and Non-Thread Safe for Windows:
What is thread safe or non-thread safe in PHP?
I saw different binaries for PHP, like non-thread or thread safe? What does this mean? What is the difference between these packages?
You can read about Thread Safety on en.wikipedia.org/wiki/Thread_safety Regarding PHP: Is PHP thread-safe Don’t Believe The Lies: PHP Isn’t Thread-Safe Yet Difference between PHP thread safe and non thread safe binarie There are plenty of resources available. Search here at Stackoverflow or Google.
4 Answers 4
Needed background on concurrency approaches:
Different web servers implement different techniques for handling incoming HTTP requests in parallel. A pretty popular technique is using threads — that is, the web server will create/dedicate a single thread for each incoming request. The Apache HTTP web server supports multiple models for handling requests, one of which (called the worker MPM) uses threads. But it supports another concurrency model called the prefork MPM which uses processes — that is, the web server will create/dedicate a single process for each request.
There are also other completely different concurrency models (using Asynchronous sockets and I/O), as well as ones that mix two or even three models together. For the purpose of answering this question, we are only concerned with the two models above, and taking Apache HTTP server as an example.
Needed background on how PHP «integrates» with web servers:
PHP itself does not respond to the actual HTTP requests — this is the job of the web server. So we configure the web server to forward requests to PHP for processing, then receive the result and send it back to the user. There are multiple ways to chain the web server with PHP. For Apache HTTP Server, the most popular is «mod_php». This module is actually PHP itself, but compiled as a module for the web server, and so it gets loaded right inside it.
There are other methods for chaining PHP with Apache and other web servers, but mod_php is the most popular one and will also serve for answering your question.
You may not have needed to understand these details before, because hosting companies and GNU/Linux distros come with everything prepared for us.
Now, onto your question!
Since with mod_php, PHP gets loaded right into Apache, if Apache is going to handle concurrency using its Worker MPM (that is, using Threads) then PHP must be able to operate within this same multi-threaded environment — meaning, PHP has to be thread-safe to be able to play ball correctly with Apache!
At this point, you should be thinking «OK, so if I’m using a multi-threaded web server and I’m going to embed PHP right into it, then I must use the thread-safe version of PHP». And this would be correct thinking. However, as it happens, PHP’s thread-safety is highly disputed. It’s a use-if-you-really-really-know-what-you-are-doing ground.
Final notes
In case you are wondering, my personal advice would be to not use PHP in a multi-threaded environment if you have the choice!
Speaking only of Unix-based environments, I’d say that fortunately, you only have to think of this if you are going to use PHP with Apache web server, in which case you are advised to go with the prefork MPM of Apache (which doesn’t use threads, and therefore, PHP thread-safety doesn’t matter) and all GNU/Linux distributions that I know of will take that decision for you when you are installing Apache + PHP through their package system, without even prompting you for a choice. If you are going to use other webservers such as nginx or lighttpd, you won’t have the option to embed PHP into them anyway. You will be looking at using FastCGI or something equal which works in a different model where PHP is totally outside of the web server with multiple PHP processes used for answering requests through e.g. FastCGI. For such cases, thread-safety also doesn’t matter. To see which version your website is using put a file containing on your site and look for the Server API entry. This could say something like CGI/FastCGI or Apache 2.0 Handler .
If you also look at the command-line version of PHP — thread safety does not matter.
Finally, if thread-safety doesn’t matter so which version should you use — the thread-safe or the non-thread-safe? Frankly, I don’t have a scientific answer! But I’d guess that the non-thread-safe version is faster and/or less buggy, or otherwise they would have just offered the thread-safe version and not bothered to give us the choice!
What is thread safe or non-thread safe in PHP ?
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.
For example:
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.
For example:
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.
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.
Thread Safety => enabled //or Thread Safety => disabled
PHP-What is thread safe or non-thread safe in PHP
For multithreaded webservers, such as IIS5 and IIS6, you should use the threaded version of PHP.
Non-Thread Safe:
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. … Apache + FastCGI: Non-Thread Safe. IIS: Thread Safe
Uses of thread safe and non-thread safe:
A thread-safe version should be used if you install PHP as an Apache module, as a worker MPM (multi-processing model).
The non-thread safe version should be used if you install PHP as a CGI binary.
FastCGI is a binary protocol for interfacing interactive programs with a web server.
FastCGI is a variation on the earlier Common Gateway Interface (CGI).
FastCGI’s main aim is to reduce the overhead associated with interfacing the web server and CGI programs, allowing a server to handle more web page requests at once.
What does thread safety mean when downloading PHP
Thread Safety means that binary can work in a multithreaded webserver context, such as Apache 2 on Windows.
Thread Safety works by creating a local storage copy in each thread, so that the data won’t collide with another thread.
So what do we 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.
Following Libraries are not thread safe. They are not recommended for use in a multi-threaded environment.
SNMP (Unix)
mSQL (Unix)
IMAP (Win/Unix)
Sybase-CT (Linux, libc5)
Difference between PHP thread safe and non thread safe binaries
There have been a few options available for a while to get PHP performing well on IIS.
Firstly is the use of an opcode cache such as eAccelerator which stores PHP scripts in a partically precompiled state on disk and/or memory which drastically decreases script execution time.
Another option is to configure IIS to use PHP in FastCGI mode which allows PHP processes to be recycled rather than killed off after each PHP request and also allows you to run several PHP processes at once, making PHP much much faster with the added bonus that as it is using the CGI interface there is little or no incompatibility issues with PHP extensions.
This is still the fastest way to serve PHP, and is the way the IIS Aid PHP Installer is configured to install PHP on your IIS Environment.
What the non thread safe binaries allow you to do is to configure IIS (and other Windows based webservers) to use PHP as a standard CGI interface with massively increased performance as the PHP process is not required to wait for thread syncronisation.
The performance increase is not to be sneezed at either as seen figures of upto 40% mentioned (though I’m yet to confirm myself), but it is still not as fast as the opcode/FastCGI method mentioned above.
One of the biggest catches we seen people getting themselves hooked on is that non thread safe binaries cannot be reliably used with the thread safe ones, and vise versa.
This means that (for the moment at least) you cannot use opcode cache systems such as eAccelerator to give your non thread safe PHP environment a boost in the arm as they are all currently compiled as thread safe.
If the non thread safe binaries are not as fast as what you can configure the thread safe binaries to be then what is the point you ask? Here we come back to FastCGI, and in particular the efforts Microsoft have been making over the last year or so with the development of their own FastCGI handler.
This new FastCGI handler from Microsoft enables you to configure the non thread safe PHP binaries in FastCGI mode, which is one massive shot in the arm for performance.
Easiest way to put it is using the non thread safe PHP binaries with Microsoft’s new FastCGI handler is like putting twin turbos on your car (without the inherent risk of blowing up your engine), and there is little doubt in my mind that this will be the future of PHP on IIS.
PHP Thread Safe and Non-Thread Safe for Windows:
Thread Safety means that binary can work in a multithreaded webserver context, such as Apache 2 on Windows.
Thread Safety works by creating a local storage copy in each thread, so that the data won’t collide with another thread.
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.
So for example, if you use FastCGI with Apache, go for the non-thread safe, and if you use LoadModule, go for the thread-safe version.
So it really depends on the way that you want to use PHP:
Apache + LoadModule: Thread Safe
Apache + FastCGI: Non-Thread Safe
IIS: Thread Safe
IIS + FastCGI: Non-Thread Safe
AFAIR running PHP with FastCGI is the preferable way.
It performs faster and allows for more fine-grained security configuration.