Home>Article>Backend Development> Let’s talk about golang’s coroutine

Let’s talk about golang’s coroutine

藏色散人
藏色散人 forward
2021-06-16 14:57:44 2502browse

The following is the tutorial column ofgolangto introduce and talk about golang’s coroutine. I hope it will be helpful to friends who need it!

Let’s talk about golang’s coroutine

Coroutines are application layer threads.

The application layer is a concept of the operating system relative to the kernel layer, corresponding to the operating level of the CPU. The core code of the operating system runs at ring0 level, and the application code runs at ring3 level. The level settings between the kernel and application layers ensure that some high-privilege operations can only be done by kernel code. To use these functions, applications must call the kernel code by calling the operating system's API (called system call on Linux). This call will cause the CPU to context switch from ring3 to ring0. This switch consumes some CPU time.

Threads are kernel objects of the operating system. During multi-thread programming, if there are too many threads, frequent context switching will occur. These CPU times are an additional cost. Therefore, in some highly concurrent network server programming, it is unwise to use one thread to serve one socket connection. So the operating system provides an asynchronous programming model based on the event pattern. Use a small number of threads to serve a large number of network connections and I/O operations. However, the use of asynchronous and event-based programming models complicates the writing of program code and is very error-prone. Because threads are interleaved, it also increases the difficulty of troubleshooting errors.

Coroutines are threads simulated at the application layer. They avoid the extra cost of context switching and take into account the advantages of multi-threading. Simplifies the complexity of highly concurrent programs. For example, in a highly concurrent network server, each socket is connected, and the server uses a coroutine to serve it. The code is very clear. And it takes into account performance.

So, how is the coroutine implemented?

The principle is the same as that of threads. When thread a switches to thread b, the relevant execution progress of thread a needs to be pushed onto the stack, and then the execution progress of thread b needs to be popped out of the stack. Enter the execution sequence of b. Coroutines are just applied to achieve this.

However, coroutines are not scheduled by the operating system, and applications do not have the ability or permission to perform CPU scheduling. how to solve this problem?

The answer is that coroutines are thread-based. In terms of internal implementation, a set of data structures and n threads are maintained. The real execution is still threads. The code executed by the coroutine is thrown into a queue to be executed, and these n threads are pulled out of the queue for execution. This solves the problem of coroutine execution. So how do coroutines switch? The answer is: golang encapsulates various io functions. These encapsulated functions are provided to applications, and internally call the asynchronous io functions of the operating system. When these asynchronous functions return busy or blocking, golang uses this opportunity to The existing execution sequence is pushed onto the stack, allowing the thread to pull the code of another coroutine for execution. The basic principle is this, using and encapsulating the asynchronous functions of the operating system. Including Linux's epoll, select and Windows' iocp, event, etc.

Since golang implements coroutines from multiple levels of the compiler and language base library, golang’s coroutines are the most complete and mature implementation among various languages with coroutine concepts. .

There is no pressure when one hundred thousand coroutines run at the same time. The key is that we don't write code like this. But overall, programmers can focus more on the implementation of business logic and spend less energy on these key basic components when writing Golang code.

The above is the detailed content of Let’s talk about golang’s coroutine. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete
Previous article:Share a Go json trap record Next article:Share a Go json trap record