multithreading - 为什么 Go 被认为是部分抢占式的?

标签 multithreading go scheduler goroutine preemption

我试图更好地理解 Go 上下文中抢占式合作式的定义。 preemptive multitasking 的 Wiki 状态

In computing, preemption is the act of temporarily interrupting an executing task, with the intention of resuming it at a later time. This interrupt is done by an external scheduler with no assistance or cooperation from the task.

Wiki 声明“外部调度程序”。我想这意味着调度程序更具体,因为调度程序只负责选择下一个进程。

Go 通常被称为部分抢占式,因为同步点/抢占点仅在函数调用处而不是在任何给定指令处。那讲得通。但正如 wiki 定义所述,抢占式是由外部调度程序完成的。

但不是每个进程或任务都是抢占式的,因为 CPU 可以在执行过程中停止执行任何进程以切换到另一个进程?任何澄清表示赞赏!

附录

我能想到的唯一解释是我们谈论不同的抢占级别。一个用于进程,一个用于内核/用户线程。在这种情况下,CPU 调度器选择下一个进程,但 Go 调度器负责 goroutines/threads。

最佳答案

您的附录是正确的。有不同的调度程序在这里工作。一种是操作系统调度程序,一种是应用程序级别。查看 this article .这正是您正在寻找的:

As we discussed in the first post, the OS scheduler is a preemptive scheduler. [...] The kernel is making decisions and everything is non-deterministic.

还有这个:

The Go scheduler is part of the Go runtime, and the Go runtime is built into your application. This means the Go scheduler runs in user space, above the kernel. The current implementation of the Go scheduler is not a preemptive scheduler but a cooperating scheduler. Being a cooperating scheduler means the scheduler needs well-defined user space events that happen at safe points in the code to make scheduling decisions.

结论,有两种不同的调度器。一个用于进程,一个用于 Goroutines。

关于multithreading - 为什么 Go 被认为是部分抢占式的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73915144/

相关文章:

php - 数据库设计 : schedules, 包括重复

c++ - QTcpSocket真的是全双工吗?

json - Golang 结构不会编码为 JSON

go - 如何在go中测试系统命令

go - 将 Go 项目与其他非 Go 项目一起存储

java - 如何在@scheduled spring boot 中在fixedDelay和initialDelay中给出分钟?

java - Java SDK中如何退出SQS读取

java - 在java中的线程之间共享字节数组的数组列表

c++ - 如何在 x86 程序集中将当前线程的堆栈指针更改为新线程

javascript - 调度程序年 View