Interviewer: Let’s talk about the three implementations of the thread model

Hello everyone, my name is Mu Chuan

1. What is the thread model?

The method used for thread creation, management, scheduling, etc. is called the thread model. Thread models are generally divided into the following three types: 内核级线程模型, 用户级线程模型, 两级线程模型. The difference between them lies in the correspondence between user threads and kernel threads. User-level threads are managed by applications. Kernel-level threads are managed by the operating system.

User threads will only be mapped to designated kernel threads before they can be scheduled and executed by the CPU. The three thread models introduced below refer to the three ways in which user threads are mapped to kernel threads.

2. Three thread models

Kernel-level threading model (1:1)

One user thread corresponds to one kernel thread. This is the easiest to implement. The scheduling of coroutines is completed by the CPU.

64298973a141aeeb37e1ffae5937ecb2.jpeg

advantage:

  • Easiest to implement

  • Ability to utilize multiple cores

  • If one thread in the process is blocked, it will not block other threads and can switch to other threads in the same process to continue execution.

shortcoming:

  • The cost of context switching is high, and creation, deletion and switching are all completed by the CPU (to create a user thread, you need to create a kernel thread and make a system call)

User-level threading model (N: 1)

All threads in 1 process correspond to 1 kernel thread e211cf3e7ffc09d79d3ff74fa2fdb7dd.jpeg. Advantages:

  • The cost of context switching is low, and coroutine switching can be completed in user mode.

shortcoming:

  • Unable to take advantage of multiple cores

  • Once the coroutine blocks, causing the thread to block, other coroutines of this thread cannot execute.

Two-level thread model (M: N)

M threads correspond to N kernel threadsaf9b6a4381c4494118f58d5617dc1a1c.jpeg

advantage:

  • Ability to utilize multiple cores

  • Context switching is cheap

  • If one thread in the process is blocked, other threads will not be blocked, and other threads in the same process will be switched to continue execution.

shortcoming:

  • The most complex to implement

3. Summary

What Go implements is the two-level thread model (M:N), to be precise, the GMP model, which is an improved implementation of the two-level thread model, allowing it to more flexibly schedule between threads.

Finally, let me advertise my original Go interview booklet. If you are engaged in Go-related development, you are welcome to scan the code to buy it. The current buyout is 10 yuan. Add the WeChat payment screenshot below to send an additional copy of your own recorded Go interview question explanation video.

d4d0984e15314cdbe139f352d203016d.jpeg

1b05983f5641b085012b5736f14b3335.png

If it is helpful to you, please help me click to watch or forward it. Welcome to follow my official account

Guess you like

Origin blog.csdn.net/caspar_notes/article/details/133802083