What is relationship between goroutine and thread in kernel and user state

后端 未结 1 831
别跟我提以往
别跟我提以往 2020-12-16 03:48

I am confused by goroutine, user thread, and kernel thread concepts

  1. From effective go introduce goroutine, so what does os threads means which

1条回答
  •  囚心锁ツ
    2020-12-16 04:46

    Let's include the picture from the go-scheduler page you linked to.

    And establish the terminology:

    • M: OS thread, can also be called a kernel thread
    • P: processor, or scheduling context
    • G: Goroutine

    goroutines are what we are most familiar with in Go, and could be considered user threads. A more technical name for those is Green Threads.

    P is used to perform the mapping from many goroutines to many OS threads. There is one per OS thread, and the number is determined by the value of GOMAXPROCS (by default, the number of CPUs as reported by your system).

    So, to answer your questions in order:

    • OS thread means kernel thread
    • GOMAXPROCS defaults to the number of cores, but you can change that. Just because you can run on all cores does not mean you're not leaving CPU time for other processes. Concurrency usually involves a lot of waiting for IO. Even if you're going crazy hashing things, the kernel scheduler will boot you off to run other things.
    • there are as many OS threads as needed. Looking at ps -eL, my system currently has 1434, some of them actual kernel jobs, some for my go program.

    You can find a really good explanation of OS vs Green threads in this answer

    0 讨论(0)
提交回复
热议问题