Php windows thread safe or non thread safe

What Is Thread Safe or Non-Thread Safe in 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 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 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

PHP manual has nice installation instructions.

AFAIR running PHP with FastCGI is the preferable way, it performs faster and allows for more fine-grained security configuration.

PHP Use thread safe or not

You should look at the PHP docs:

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.

What are the technical differences between the Thread Safe and Non Thread safe PHP Windows Installation Packages?

After a lot of research, I’ve managed to find my own answers to this question.

In its most basic form, the answer is: What version of PHP you should install comes down what webserver you are running.

Here’s a deeper explanation of the terms used in picking a version of PHP based on what I learned:

VC6 vs VC9

Firstly, different versions of Apache for Windows are compiled with different compilers. For example, the versions on Apache.org are designed to be compiled using Microsoft Visual C++ 6, also known as VC6. This compiler is very popular, but also very old. (It dates back to 1998.)

There are different versions of Apache made for different compilers. For example, the versions available for download from ApacheLounge.com are designed to be compiled with the popular and more much recent compiler, Microsoft Visual C++ 9 from 2008. Also known as VC9.

(Note: These two compilers are the two most popular options. So while it’s possible to have a VC7, VC8, etc. compiled version of Apache, it’s unlikely that you’ll come across them.)

The use of this more recent compiler (VC9) is important because the latest versions of PHP are only being distributed in VC9 form (although older versions are still available for VC6).

On top of that, according to ApacheLounge there are numerous improvements when using a version of Apache compiled with VC9, «in areas like Performance, MemoryManagement and Stability».

If that wasn’t enough, the developers of PHP made the following statement on their site:

Windows users: please mind that we do
no longer provide builds created with
Visual Studio C++ 6 (VC6). It is
impossible to maintain a high quality
and safe build of PHP for Windows
using this unmaintained compiler.

We recommend the VC9 Apache builds as
provided by ApacheLounge
.

All PHP users should note that the PHP
5.2 series is NOT supported anymore. All users are strongly encouraged to
upgrade to PHP 5.3.6.

In all, this is an extremely compelling argument to use VC9 versions of Apache and PHP, if you ask me.

So if you’re using a version of Apache from the official Apache site, it will be compiled with VC6, and as such, you should use the older version of PHP for that compiler. If you’re using a version of Apache compiled with VC9, like the one available on ApacheLounge.com, you can use the latest version of PHP (for VC9).

For me, running a local development environment, it would be preferable to have the latest version of PHP, so a VC9 version of Apache is required, so I can use the VC9 version of PHP.

Thread Safe vs Non Thread Safe

Once again this comes down to your webserver. By default Apache is installed on Windows as Module, but it can be changed to run as FastCGI. There’s plenty of differences between the two, but essentially FastCGI is more modern, faster, more robust, and more resource hungry. For someone running a local development environment, FastCGI might be overkill, but apparently lots of hosting companies run as FastCGI for the reasons I’ve stated, so there are good arguments for doing so in a development environment.

If you’re running Apache (or IIS) as FastCGI (or CGI) then you want the Non Thread Safe version of PHP. If you’re running Apache as default (as a Module), then you’ll want the more traditional Thread Safe version.

Please note: This all only applies to Windows users.

I’m not going to bother with FastCGI (unless someone convinces me otherwise), so for me, I want the VC9 Thread Safe version of PHP.

Further reading:

  • Official statement regarding PHP and VC6
  • Difference between PHP thread safe and non thread safe binaries
  • FastCGI at Wikipedia
  • FastCGI for IIS
  • Visual C++ at Wikipedia
  • Compile your own PHP (explanation of VC6/VC9)

Find if the installed PHP is threadsafe or nonthreadsafe?

Open a phpinfo() and search for the line Thread safety. For a thread-safe build you should find enable.

As specified in the comments by Muhammad Gelbana you can also use:

What does thread safe mean in a PHP context?

Thread safe is a good thing, it means whilst there may be multiple concurrent threads, they are talking to each other in a safe way that won’t have race conditions, concurrency issues, etc.

Thread safety is a computer programming concept applicable in the
context of multi-threaded programs. A piece of code is thread-safe if
it only manipulates shared data structures in a thread-safe manner,
which enables safe execution by multiple threads at the same time.
There are various strategies for making thread-safe data structures.

to use thread safe or non thread safe php?

If you use mod_php and MPM that creates threads to handle requests (instead of processes), you should go with thread-safe, as otherwise you might get a bunch of undesired side effects when multiple PHP scripts are running simultaneously in different threads in same process space. Apache2 uses the mpm_winnt MPM by default on Windows, which creates threads to handle incoming requests. So if you use that, go with the thread safe binary. Please note though, that even if PHP itself is thread-safe, the libraries/extensions you use might not be. So you might still have problems with mod_php and a threaded MPM, even with thread safe PHP binaries. For more information, check this FAQ entry: http://fi.php.net/manual/en/faq.installation.php#faq.installation.apache2

However if you’re not using mod_php (eg. you’re using FCGI), or you’re using mod_php but are using a MPM that spawns processes instead of threads to handle request (eg. the prefork MPM), then you should use the non-thread safe binary, as it’s more efficient than the thread safe version, and since PHP scripts run in their own process space when you’ve got your webserver setup this way, there’s no reason for the extra overhead of thread safe binaries.

Источник

Почему существует 2 версии PHP (Non Thread Safe и Thread Safe)?

Здравствуйте.
На сайте PHP для загрузки доступны 2 версии: Non Thread Safe и Thread Safe.
Если я правильно понимаю, версия Thread Safe нужна для того, чтобы была возможность использовать расширение pthreads. Но если есть версия Thread Safe зачем до сих пор собирается версия с Non Thread Safe?
Какие недостатки есть у версии Thread Safe (больше накладных расходов, уменьшенная производительность, etc)?
Благодарю за ответы.
С уважением.

zoonman

Для тех, кто не знает английский.

Версию сервера нужно выбирать в зависимости от того, как вы интегрируете PHP c веб-сервером.
Например, когда вы используете mod_php, у вас модуль и PHP всегда загружены в память и каждый запрос обрабатывается в отдельном потоке. Из-за этого mod_php работает быстрее, чем CGI. В данном случае нужно использовать потоко-безопасную реализацию (thread-safe) поскольку каждый поток имеет доступ к памяти другого потока.

Когда вы работаете с IIS, у вас PHP запускается ввиде отдельного процесса через CGI и в таком случае потоко-безопасность не имеет смысла, поскольку у вас 1 процесс и фактически 1 поток созданный этим же процессом. Операционная система изолирует память процессов по умолчанию.

В случае модели с FastCGI, потокобезопасность также не имеет смысла, поскольку тотже PHP-FPM держит несколько отдельных процессов PHP в памяти, но не потоков.

Источник

Читайте также:  Php проверить содержит ли строка символы
Оцените статью