Home >Common Problem >What is coroutine in golang
The coroutine in golang is goroutine. The difference is that Golang encapsulates and processes goroutine scheduling in many aspects such as runtime and system calls. When encountering long-term execution or system calls, it will actively The CPU of the current goroutine is transferred so that other goroutines can be scheduled and executed. Golang natively supports coroutines from the language level. You can create a coroutine by adding the go keyword in front of a function or method.
The operating environment of this tutorial: Windows 10 system, go1.20.1 version, Dell G3 computer.
For coroutines (user-level threads), this is transparent to the kernel, that is, the system does not know the existence of coroutines, and is completely scheduled by the donkey's own program, because it is the user program itself. control, then it is difficult to forcefully switch the CPU control to other processes/threads like preemptive scheduling. Usually only collaborative scheduling can be performed. The coroutine needs to actively transfer control rights before other coroutines can be controlled. Executed to.
The difference between go-routine and coroutine
Essentially, goroutine is a coroutine. The difference is that Golang encapsulates and processes goroutine scheduling in many aspects such as runtime and system calls. When encountering long-term execution or system calls, it will actively transfer the CPU (P) of the current goroutine to other goroutines. It can be scheduled and executed, which means Golang supports coroutines from the language level. A major feature of Golang is that it supports coroutines natively from the language level. You can create a coroutine by adding the go keyword in front of a function or method.
Comparison in other aspects
1. Memory consumption
Each go-routine (coroutine) occupies much less memory by default than Java and C threads.
go-routine. 2KB.
Thread: 8MB.
2. In terms of scheduling overhead for switching between threads and go-routine
In terms of thread/go-routine switching overhead, go-routine is far smaller than threads.
Thread: involves mode switching (switching from user mode to kernel mode), refreshing of 16 registers, PC, SP.. and other registers, etc.
go-routine: Only three register values are modified - PC/SP/DX.
2. The underlying implementation principle of coroutines
Threads are kernel objects of the operating system. During multi-thread programming, if there are too many threads, frequent context switches will occur. These CPU times are An additional expense. 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 /0 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.
Coroutine is a thread simulated at the application layer. It avoids the extra cost of context switching and takes 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.
The above is the detailed content of What is coroutine in golang. For more information, please follow other related articles on the PHP Chinese website!