Home > Backend Development > Golang > How does golang implement coroutines?

How does golang implement coroutines?

coldplay.xixi
Release: 2020-06-20 09:34:16
Original
4674 people have browsed it

How does golang implement coroutines?

#How does golang implement coroutines?

Golang’s method of implementing coroutines:

There are processes, threads, and IO multiplexing methods to implement concurrent programming. (We do not distinguish between concurrency and parallelism here. If the CPU is multi-core, it may be performed on multiple cores at the same time. We call it parallelism. If it is a single-core CPU and needs to be queued to switch, we call it concurrency.)

Processes are computer resources The smallest unit of allocation, the process is an abstraction of processor resources and virtual memory (1), while virtual memory is an abstraction of main memory resources and files (2), and files are an abstraction of I/O devices. Threads are the smallest unit of computer scheduling and share computer resources allocated by the same process.

The above is about in-depth understanding of computer systems.

In summary, what actually implements concurrency is threads. First of all, each process has a main thread, because a thread is the smallest unit of scheduling. You can have only one thread, but you can also create several more threads. Thread scheduling requires CPU switching and kernel layer context switching. If You run thread A, then switch to thread B, and the kernel call starts. The CPU needs to retain the context of thread A, then switch to thread B, and then transfer control to your application layer scheduling. Process switching also requires the kernel to switch, because the main thread of the C process is switched to the main thread of the D process.

Then processes need to communicate, and their resources are not shared. At this time, IPC (Inter-Process Communication, inter-process communication) needs to be used. Commonly used ones include semaphores, shared memory, and sockets. In fact, Baidu It is said that there are six types, and multiple threads of the same process share resources and are much easier to communicate than processes. Now many programming languages ​​​​such as Java have this kind of thread interaction implementation.

As for IO multiplexing, it actually means maintaining a queue, and then letting one thread or multiple threads go to the queue to get tasks to complete. why? Because the number of threads is limited, and communication between threads requires some resources, the kernel must frequently switch contexts.

One thread's IO multiplexing, typically Redis and Nodejs, does not need to switch contexts at all, one thread can dominate the world. The IP multiplexing of multiple threads is the implementation method of Golang coroutine. The coroutine manages the threads by itself, controls the threads to a certain number, and then constructs a rule state machine to schedule tasks.

Recommended tutorial: "go language tutorial"

The above is the detailed content of How does golang implement coroutines?. 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