Home > Article > Backend Development > What does php multithreading mean?
PHP does not support multi-threading by default. By installing the pthreads extension, let its parameters specify the thread-safe way to compile PHP to support multi-threading. However, thread safety must be considered during use. question.
Thread is the smallest unit for operation scheduling in the operating system. It is included in the process and is the actual operating unit in the process. What I will share today is that thread knowledge is related to PHP and has certain reference value. I hope it will be helpful to everyone
[Recommended courses: PHP Tutorial】
Understanding of multi-threading
The so-called multi-threading is in a Multiple threads can run concurrently in a process, and each thread can perform different tasks in parallel. Multi-threading greatly improves the execution efficiency of the program. A multi-thread has a greater probability of being scheduled by the operating system than a single thread. And more efficient. Multiple threads can run simultaneously on multiple cores of a multi-core CPU, speeding up operating efficiency. And communication between threads is simpler.
PHP multi-threading
By default, PHP does not support multi-threading. To use multi-threading, you need to install extensions. Currently, commonly used extensions include pcnlt, POSIX, pthreads, but the most commonly used one is the pthreads extension, which uses parameters to specify the thread-safe way to compile PHP to support multi-threading.
Before using threads, you must first consider the issue of thread safety. Thread safety refers to the ability to correctly handle shared variables between multiple threads when a function or function library is called in a multi-threaded environment. , so that the functions of the program can be completed correctly
Example:
Due to the existence of shared variables in multi-threads, it is likely to cause the following problems:
存在一个全局数组:$arr = array('a'); A 线程获取数组长度为1; B 线程获取数组长度为1; A 线程pop出数组元素 $a = array_pop($arr); $a = 'a'; B 线程也pop数组元素 $b = array_pop($arr); $a = null; 但是此时B线程内就出现了错误事件,虽设置了数组长度大于0,但是没有 pop值来
PHP realizes thread safety
PHP realizes thread safety mainly through the TSRM mechanism. Through this mechanism, global variables and static variables can be isolated, and a copy of global variables and static variables is copied to each thread. . Each thread uses a backup of the main thread. This avoids variable conflicts and does not cause thread safety issues.
PHP's encapsulation of multi-threading ensures thread safety, so developers do not need to consider read and write conflicts, which also makes the code safer. However, there are also disadvantages to this. For example, when the sub-thread starts running, the main thread can no longer adjust the running details of the sub-thread and loses the ability to pass messages.
Summary: That’s it for this article That’s all, I hope it’s helpful to everyone.
The above is the detailed content of What does php multithreading mean?. For more information, please follow other related articles on the PHP Chinese website!