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!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
