In swoole, because coroutine switching is serial, only one coroutine can be run at the same point in time. When one coroutine is running, other coroutines will stop working, so swoole's coroutine The process is based on single thread.
The operating environment of this tutorial: Windows10 system, Swoole4 version, DELL G3 computer
Why is the swoole coroutine single-threaded
Simply put, coroutines are lightweight threads that collaborate with lighter concurrency implemented by programmers hosted under threads
As the number of programmers increases, the big guys continue to explode. With the growth, of course some people began to think that threads are not easy to use, so what should we do? Of course, based on the concept of threads, we will implement a set of lightweight threads that are more lightweight and better to cheat stars (in fact, coroutines cannot Completely considered a thread, because a thread can have multiple coroutines)
The difference between coroutines and threads
Essence
Thread Kernel State
Coroutine User Mode
Scheduling method
The scheduling method of threads is system scheduling. Commonly used scheduling strategies include time-sharing scheduling and preemptive scheduling. To put it bluntly, the scheduling of threads is completely out of your control
The scheduling method of coroutines is collaborative scheduling and is not controlled by the kernel and is switched by free policy scheduling
As mentioned above, coroutines are in user mode. So the so-called collaborative scheduling can be directly understood as a scheduling method written by programmers, that is, I can schedule how I want without being scheduled through the system kernel.
A brief understanding of swoole's coroutine
Since we plan to have a brief understanding of swoole's coroutine, we must know the swoole's coroutine model.
Swoole's coroutine is based on single thread. It can be understood that the switching of coroutines is serial, and only one coroutine is running at the same time.
Swoole's coroutine is single-threaded in the underlying implementation, so only one coroutine is running at the same time. Work,execution of coroutines is serial. This is different from threads. Multiple threads will be scheduled by the operating system to multiple CPUs for parallel execution.
When one coroutine is running, other coroutines will stop working. The current coroutine will hang when performing blocking IO operations, and the underlying scheduler will enter the event loop. When there is an IO completion event, the underlying scheduler resumes the execution of the coroutine corresponding to the event.
The utilization of CPU multi-core still relies on the multi-process mechanism of the Swoole engine.
Recommended learning: swoole tutorial
The above is the detailed content of Why is the swoole coroutine single-threaded?. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

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