PHP Thread Safety

Source: Internet
Author: User
Tags zend

See Zend Debuger has a non-thread-safe version, only to know that PHP launched a non-thread-safe version. And I didn't know anything about non-thread safety before:
Another article seems to say that this has something to do with fastcgi.
It's a piece of text, but I didn't see it:
PHP itself is thread-safe. A service process can safely provide support for multi-request threads
Some extensions do not follow
For example, in a thread-safe extension, the definition of a global variable is not defined directly outside of a function like a normal C program, but is defined between the macro zend_begin_module_globals and zend_end_module_globals. Extensions that require ZTS (Zend Thread Safe) need to include TSRM.h header files and define TSRMG macro values
In an extension that does not support thread safety, simply assume that a service process has only one request in the active state and that there is no conflict, then the global variable can simply be initialized in the Rinit function (Rinit represents the request) and logged out in Rshutdown:

Code:php_rinit_function (EXT)
{
Counter = 0;
}
Php_function (EXT)
{
Return_long (counter++);
}
This is a very simple counter. As long as the request does not end, each call to Ext will trigger the counter self-increment.
When in a multithreaded environment, this program can be seriously confused, counter will become erratic, because there is no way to predict the thread's trigger and end order and time. This indicates that this extension is not thread-level safe.


Multithreading, Apache 1.3 and Apache 2.0

If you've been using Apache and PHP for a while, you've probably seen a warning in the installation documentation that says "don't use Apache 2.0.x and PHP in a production environment, not on Unix or Windows." In a PHP 5.0.2 package on a Windows system, this warning message can be found in line No. 745 of the Install.txt file. We need to understand what this problem is, so we can decide whether to use Apache 2.0 or IBM HTTP Server 2.0.

Apache 2.0 can be configured to run in two ways: threaded and non-threaded. When running as a thread-taking server, multiple threads can be active in the server at the same time, and response information can be generated for multiple users at one time. In general, this can improve the responsiveness of the server to make better use of large hardware with multiple processors. But it also introduces a risk. Each software hierarchy that a server invokes must be secure when it is called to multiple users at the same time. Although the WEB server itself, the PHP interpreter, and the PHP extension are safe to call, some PHP extensions use libraries written in other languages, such as the C language, which are not all thread-safe.

On the Apache Web page you can find a discussion of this issue, with some suggestions, and a way to find out which C libraries your PHP extension might be using and which are thread-safe, see the Resources section.

In practice, many people will choose to avoid this problem, but the following two methods: either single-threaded mode using Apache 2.0, or using Apache 1.3, it always runs in single-threaded mode. Although Apache 1.3 and 2.0 also have some other differences, such as Apache 2.0 can support IPV6, but so far, the biggest difference between the two is the threading problem, so preserving the use of Apache 1.3 server is not as degraded as it sounds.

What is this problem in IBM HTTP Server? IBM uses threading mode to compile IBM HTTP Server from Apache 2.0: This is faster, but may not be secure when using non-thread-safe extensions. Because IBM does not release the source code at the same time, and the choice of threading and non-threading patterns are selected at compile time, you cannot choose to recompile IBM HTTP Server 2.0 in a pattern that does not use threads as an end user. However, at the time of writing, IBM is releasing IBM HTTP Server versions 2.0 and 1.3 at the same time, so you can choose to use a single-threaded version of the 1.3-based server.


This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/mynameisyoudi/archive/2008/10/07/3029496.aspx

PHP Thread Safety

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.