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.

线程安全就是能够在多线程环境下正常工作。这取决于web server在处理并行访问时是否用到多线程。

如何选择?

PHP手册给出答案:

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.

不论在什么平台、用什么web server,只要是用cgi/fastcgi方式运行PHP,都用非线性安全。

这意味着nginx必然配合非线程安全的PHP,IIS则要用线性安全的PHP。Apache有两种运行模式:如果用fastcgi模式,则配合非线性安全PHP,如果用LoadModule模式,则要用线性安全PHP。后者应该是大多数Apache使用者的选择。

参考资料:

  • http://php.net/manual/en/faq.obtaining.php#faq.obtaining.threadsafety
  • http://stackoverflow.com/questions/1623914/what-is-thread-safe-or-non-thread-safe-in-php
  • http://stackoverflow.com/questions/7204758/php-thread-safe-and-non-thread-safe-for-windows

你可能感兴趣的:(PHP的线程安全与非线程安全)