The smallest unit that the operating system can perform operation scheduling is included in the process and is the actual operation unit of the process. A thread refers to a single sequence of control flow in a process. Multiple threads can run concurrently in a process, and each thread executes multiple tasks in parallel.
A multi-threaded program is more likely to be called by a program than a single-threaded program, so multi-threaded programs are generally more efficient than single-threaded programs.
Multiple threads of a multi-threaded program can run on multiple cores of a multi-core CPU, fully taking advantage of the multi-core CPU.
Features:
The system overhead of creating and switching threads is smaller than that of processes, so it will be more efficient than multiple processes to a certain extent.
Threads naturally share memory space, and communication between threads is simpler, avoiding the introduction of new complexity by process IPC.
Applicable scenarios:
Random use of multi-threading cannot improve the execution efficiency of the program. The creation and destruction of threads, context switching, and thread synchronization all have performance costs.
1. I/O blocking will cause task scheduling in the operating system and block the current task. Therefore, when there is a lot of I/O in the code, the code can be parallelized when using multiple threads. For example, reading an entire file multiple times, or requesting multiple network resources.
2. Multi-threading can make full use of the CPU, so when there are multiple large calculation codes, you can also use multi-threading to execute them in parallel.
Multi-threading of PHP:
PHP does not support multi-threading by default. You need to add the pthread extension.
You must use the --enable-maintainer-zts parameter to recompile PHP. This parameter specifies the thread safety method when compiling PHP.
Thread safety:
Thread safety is a term in programming, which means that when a function or function library is called in a multi-threaded environment, it can correctly handle shared variables between multiple threads, so that the program functions correctly Finish.
In traditional multi-threading, since multiple threads share variables, the following problems may occur:
There is a global array $arr = array('a');;
A thread obtains the array length is 1;
Thread B gets the array length to be 1;
Thread A pops out the array element $a = array_pop($arr); $a = 'a';;
Thread B also pops out the array element$ b = array_pop($arr); $a = null;;
At this time, a supernatural event occurred in the B thread. The array length was obviously greater than 0, or nothing was popped;
PHP implementation:
Use The TSRM mechanism isolates global variables and static variables
Copies global variables and static variables to each thread. Each thread uses a backup of the main thread, avoiding variable conflicts and no threads. Security Question.
Problems that arise:
Once the sub-thread is running, the main thread can no longer make detailed adjustments to the sub-thread. Threads lose the ability to pass messages between threads through global variables.
Using the TSRM mechanism to allocate and use variables will incur additional losses, so ZTS (non-thread safety) is not required in a multi-threaded PHP environment
Classes and methods:
PHP encapsulates threads into Threads Classes and threads are created by instantiating a thread object. Due to the encapsulation of the class, the smart use of variables
is passed in through the constructor, and the thread's operation results also need to be passed out through class variables.
Thread method:
run(): This method is an abstract method. Each thread must implement this method. After the thread starts running, the code in this method will be automatically executed;
start(): Call this method in the main thread to start running a thread;
join(): Each thread is executed asynchronously relative to the main thread. Calling this method will wait for the thread to end;
kill(): Force the thread to end ;
isRunning(): Returns the running status of the thread. It will return true when the thread is executing the code of the run() method;
Due to the implementation of thread safety, after PHP multi-threads start running, they can no longer pass the shared memory Space communication, threads cannot be reused through inter-thread communication.
Related recommendations:
Application of PHP multi-threaded pipeline communication
How to implement Web Worker in H5 multi-threading
php method to implement asynchronous calling of multi-threads
The above is the detailed content of Detailed explanation of php multi-threading examples. For more information, please follow other related articles on the PHP Chinese website!