Is PHP thread-safe?

PhpMultithreadingThread Safety

Php Problem Overview


  • Is PHP (as of 5.2) thread-safe on Linux/UNIX?
  • Would it be possible to use it with Apache Worker-MPM or Event-MPM?

The facts I gathered so far are inconclusive:

  • Default binaries included in most distributions have ZTS disabled, so I'm aware, that I'd have to recompile them.
  • In theory Zend Engine (core PHP) with ZTS enabled is thread-safe.
  • It's said that some modules might not be thread-safe, but I haven't found any list of modules that are or that are not.
  • PHP FAQ states pretty much same as above.

What's your experience?

It's not only about segmentation faults ("access violations" in Windows nomenclature). There is a lot more to thread safety.

Php Solutions


Solution 1 - Php

I know gettext and set_locale is not threadsafe. PHP should not be used with a threaded MPM.

PHP Isn't Thread-Safe Yet.
Running PHP not threaded.

Solution 2 - Php

See Where can I get libraries needed to compile some of the optional PHP extensions? for a list of thread-safe and nonthread-safe extensions (* marked are not thread-safe and others are).

Solution 3 - Php

A better question might be, "Is the following PHP code going to trigger access violations if MPM is used?" Or, "Have you experienced odd behavior likely attributed to concurrency issues using the following functions?"

Otherwise, it's Russian roulette. If you're using some packaged application, it may work just fine now but break a month from now when a new version of the application comes out.

I strongly advise against using MPM with PHP in general. However, if you have some small code to run, you could post it, and we could tell you if you're going to hit a pitfall.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvartecView Question on Stackoverflow
Solution 1 - PhpOISView Answer on Stackoverflow
Solution 2 - PhpAbdolHoseinView Answer on Stackoverflow
Solution 3 - PhpTim PostView Answer on Stackoverflow