search
HomeBackend DevelopmentPHP TutorialDetailed explanation of php multi-threading examples
Detailed explanation of php multi-threading examplesMar 12, 2018 pm 03:09 PM
phpExampleDetailed explanation

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

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

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

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

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

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

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

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

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

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

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

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

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

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

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

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

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool