Home > PHP Framework > Swoole > Why is the swoole coroutine single-threaded?

Why is the swoole coroutine single-threaded?

WBOY
Release: 2022-03-14 15:46:01
Original
2640 people have browsed it

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.

Why is the swoole coroutine single-threaded?

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template