What is thread safety

Posted May 25, 20203 min read

  1. Development version

There are two versions in the PHP installation version, the thread-safe version and the thread-unsafe version

The following is an explanation of the development version:

ts(Thread-Safety) is thread safety. When multi-threaded access is used, a locking mechanism is adopted. When a thread accesses certain data of this class, it is protected, and other threads cannot access until the thread finishes reading, other threads Before use. There will be no data inconsistency or data pollution. Select this version when PHP is loaded in ISAPI mode. Select this version when PHP is loaded in ISAPI mode.
nts(None-Thread Safe) is non-thread safe, that is, it does not provide data access protection. It may happen that multiple threads change the data one after another, resulting in dirty data. PHP chooses this version when running in fast cgi mode, which has better Performance

Where can I see it? Printing phpinfo() as shown below will display some configuration information:
1590417033.png

Fortunately, when we use the PHP version, we can distinguish between thread-safe and non-thread-safe. This is selected when the ISAPI method is loaded, proposed by Microsoft, so it can only be run on the win platform. Linux is running a non-thread-safe version. No matter what platform or web server is used, as long as PHP is run in cgi/fastcgi mode, non-linear security is used.

Causes of unsafe threads

There will be no thread safety issues during the development of a single thread. Only when multiple threads access shared resources during the use of multiple threads will thread safety issues arise. As long as the resource has not changed, it is safe for multiple threads to read the same resource.

Combine the application scenarios of overbought and oversold. Imagine that threads A and B execute orders for the same commodity at the same time.

A:The stock of the read product is 1
B:The stock of the read product is 1
A:The order is successfully deducted inventory -1
B:The order is successfully deducted inventory -1
B:The operation was successful and the inventory was reduced to -1

At this time, you will find that only one item in stock has been sold twice. There was a phenomenon of overselling. Combined with the analysis of the causes of unsafe threads:As long as the resources have not changed, the threads are safe, and the inventory of the resources accessed now has changed, causing the threads to become unsafe.

The common problem of oversold and overbought caused by high concurrency is, in more professional terms, thread insecurity.
So the question is, how to make the thread safe? This article analyzes from the code level(for other levels of analysis, please read Xiaobian's other articles). You can start from two perspectives:resources are not shared by multiple threads and sequential execution(locking)

Local variables

Local variables are stored in the thread's own stack. In other words, local variables will never be shared by multiple threads. Therefore, local variables of the basic type are thread-safe. The following is an example of a local variable of the basic type:

public void someMethod() {
   $value = 0;
   $value ++;
}

Determine if the resource object is thread safe

If the creation, use, and destruction of a resource are completed in the same thread, and will never leave the control of the thread, the use of the resource is thread-safe.

References are not thread safe!

It is important to remember that even if an object is a thread-safe immutable object, the reference to this object may not be thread-safe

class Calculator {
    private $currentValue = null;

    public function getValue() {
        return $this.currentValue;
    }
    //The reference variable of the incoming parameter will assign the reference variable to currentValue. As the reference variable changes, the currentValue will also change
    public function setValue(& $newValue) {
        $this.currentValue = $newValue;
    }
}

At last

Then the question arises, is it over-bought and over-sold above, and after using thread-safe writing to achieve thread-safety? The result is that even if the program code is thread-safe, the inventory of the merchandise exists in the database, and if two safe threads read the database inventory to modify it, there will be a competitive relationship. At this time, it is achieved by using a locking method. Refer to other articles in Xiaobian.