views:

3548

answers:

2
+8  Q: 

Is PHP thread-safe

  • 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 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 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?

EDIT: It's not only about segfaults ("access violations" in windows nomenclature). There is a lot more to thread safety.

+6  A: 

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

PHP Isn't Thread-Safe Yet.

OIS
Yes, I've read that article before. But it doesn't give much facts. Also segfaults (or "access violation") hasn't really got much to do with thread safety.
vartec
Yes they do. If the segfaults are as a result of memory access violations due to incorrect threaded access to shared variables, then that's exactly the problem you are looking to avoid. It's *not only* about segfaults/access violations, but if it does segfault then don't bother looking any further because it's definitely not thread safe.
Computer Guru
+2  A: 

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, its Russian roulette. If your 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 your going to hit a pitfall.

Tim Post
actually code may be not thread-safe and never cause any segfault/access violation.
vartec
@vartec, yes, I edited my answer.
Tim Post