Goroutines和调度程序

huangapple go评论72阅读模式
英文:

The Goroutines and the scheduled

问题

Go协程是协作调度的,而不是依赖内核来管理它们的时间共享。

英文:

i didn't understand this sentence plz explain me in detail and use an easy english to do that
Go routines are cooperatively scheduled, rather than relying on the kernel to manage their time sharing.

答案1

得分: 3

免责声明:这只是对内核和Go运行时中调度概念的粗略和不准确的描述,旨在解释概念,而不是对真实系统进行精确或详细的解释。

正如你可能(或者可能不)知道的那样,CPU实际上不能同时运行两个程序:CPU只有一个执行线程,每次只能执行一条指令。早期系统的直接结果是,你不能同时运行两个程序,每个程序都需要(系统层面上)一个专用线程。

目前采用的解决方案称为伪并行性:给定一定数量的逻辑线程(例如多个程序),系统将在一定的时间内执行一个逻辑线程,然后切换到下一个线程。使用非常小的时间间隔(大约是毫秒级),你给人类用户产生了并行性的错觉。这个操作称为调度。

Go语言并不直接使用这个系统:它自己实现了一个调度器,运行在系统调度器之上,并调度goroutine的执行,绕过为每个例程使用真实线程的性能开销。这种类型的系统称为轻量级/绿色线程

英文:

Disclaimer: this is a rough and inaccurate description of the scheduling in the kernel and in the go runtime aimed at explaining the concepts, not at being an exact or detailed explanation of the real system.

As you may (or not know), a CPU can't actually run two programs at the same time: a CPU only have one execution thread, which can execute one instruction at a time. The direct consequence on early systems was that you couldn't run two programs at the same time, each program needing (system-wise) a dedicated thread.

The solution currently adopted is called pseudo-parallelism: given a number of logical threads (e.g multiple programs), the system will execute one of the logical threads during a certain amount of time then switch to the next one. Using really small amounts of time (in the order of milliseconds), you give the human user the illusion of parallelism. This operation is called scheduling.

The Go language doesn't use this system directly: it itself implement a scheduler that run on top of the system scheduler, and schedule the execution of the goroutines itself, bypassing the performance cost of using a real thread for each routine. This type of system is called light/green thread.

huangapple
  • 本文由 发表于 2015年8月24日 09:36:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/32173352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定