英文:
How does Go decide when to context switch between goroutines?
问题
Go语言是如何调度goroutine的呢?它是仅在通道请求和I/O时切换,还是有一个周期性的协程切换循环呢?
英文:
I am curious as to how the Go language schedules goroutines. Does it switch only during channel requests and I/O or does it have a periodic coroutine switching loop?
答案1
得分: 8
Go目前还没有预先调度程序,但计划在1.2版本中添加一个。因此,Go在仅进行CPU计算时不会切换上下文,只会在进行I/O操作时切换上下文(如果内存读取不在寄存器中,则也被视为I/O操作)。你可以在Issue 543 - preemptive scheduling中阅读一些相关讨论。
英文:
Go does not have a preemptive scheduler yet, but one is planned for 1.2. So no, Go will not switch context during CPU-only calculations, only during I/O (reading from memory is also considered I/O if it isn't in a register already). You can read some discussion about it in Issue 543 - preemptive scheduling.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论