Home > Common Problem > What is the difference between golang threads and coroutines?

What is the difference between golang threads and coroutines?

DDD
Release: 2023-06-16 13:15:40
Original
1136 people have browsed it

The difference between golang threads and coroutines: 1. Different scheduling methods. Threads are preemptively scheduled according to CPU time slices, and coroutines are scheduled by the go language runtime scheduler; 2. Different scheduling strategies. Thread preemptive scheduling, coroutine collaborative scheduling; 3. The context switching speed is different, the thread switching speed is about 1 to 2 microseconds, and the coroutine switching speed is about 0.2 microseconds; 4. The stack size is different, and the thread stack The size is generally 8MB, and the default go coroutine stack size is 2KB.

What is the difference between golang threads and coroutines?

The operating environment of this article: Windows 10 system, go1.20 version, dell g3 computer.

The difference between golang threads and coroutines:

1. Scheduling method

Threads: Threads are preemptively scheduled based on CPU time slices. The operating system performs thread context switching through interrupt signals (timer interrupts, I/O device interrupts, etc.). When a thread context switch occurs, it needs to be transferred from the operating system user mode to the kernel mode and the status information is saved; when switching to the next thread to be executed, the status information needs to be loaded and transferred from the kernel mode to the operating system user mode.

Coroutine: Coroutine exists in user mode and is scheduled by the go language runtime scheduler. Coroutines belong to a certain thread. Multiple coroutines can be scheduled into one thread. One coroutine may also be switched to multiple threads for execution. Therefore, coroutines and threads have a many-to-many (M:N) relationship.

2. Scheduling strategy

Thread: Preemptive scheduling. In order to balance the execution cycle of each thread, the operating system scheduler will periodically send out interrupt signals to force thread context switching.

Coroutine: Collaborative scheduling. After a coroutine has finished processing its own tasks, it can actively transfer execution rights to other coroutines and will not be easily preempted. Only after the coroutine has been running for too long, the go language scheduler will forcefully preempt its execution.

3. Context switching speed

Thread: Thread context switching requires switching between the operating system user mode and the kernel mode. The switching speed is about 1 to 2 microseconds. .

Coroutine: Coroutine is a lightweight thread in user mode. The switching of coroutine does not require switching between user mode and kernel mode, and only a few status values ​​need to be saved when switching, so the switching speed is Several times faster, about 0.2 microseconds. (About 10 times faster than the thread switching speed)

4. Stack size

Thread: The stack size of the thread is generally specified when creating, linux and mac The default stack size is generally 8MB (can be viewed through ulimit -s). 2000 threads require 16G of virtual memory.

Coroutine: The default go coroutine stack size is 2KB, and 16G virtual memory can create more than 8 million coroutines. In practice, it is common to see the existence of thousands of coroutines.

The above is the detailed content of What is the difference between golang threads and 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