Goroutines是什么意思?

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

What exactly are goroutines?

问题

我已经阅读的教程和文档都说goroutine“不完全是线程”或“轻量级线程”,但通常可以像它们是独立的线程一样处理。

那么...它们到底是什么?

英文:

The tutorials and documentation I've read all say that goroutines are "not quite threads" or "lightweight threads" but can usually be treated like they are their own separate thread.

So... What are they really?

答案1

得分: 17

几个方面使得 goroutine 与典型的操作系统线程有所区别:

  • 存在用户模式调度。当一个 goroutine 被阻塞(比如等待网络响应),Go 运行时会寻找另一个可以运行的 goroutine。这一过程不需要进入和退出内核模式,也不需要操作系统内核的调度器运行。
  • 并不仅仅是用户模式调度:为了利用多核,Go 会启动多个操作系统线程,并在所有线程上运行 goroutine,可能会在不同的操作系统线程之间移动 goroutine 以保持所有线程繁忙。如果你听说过 goroutine 被“多路复用”到操作系统线程上,就是指这个意思。
  • 它们的启动成本很低。栈的初始大小很小,只有几千字节,并根据需要增长,无论操作系统是否使用虚拟内存超额分配。
  • 它们与语言紧密结合;除了 go 语句本身之外,还有通道类型、操作和select语句用于协调 goroutine。
  • 它们缺少一些操作系统线程的特性:目前,Go 的调度器不能保证公平性,并且只有非常有限的抢占(一个空的 for{} 循环永远不会被切换出去)。

它们与许多其他术语密切相关:

  • 纤程,这个术语与用户模式调度的线程有关
  • 绿色线程,另一个用于指代用户模式调度线程的术语
  • 协程,指的是可以在代码的任意点上相互让出控制权的例程
  • 事件驱动架构,在等待异步事件(如网络 I/O)时可能会切换到其他任务,但可能不提供类似线程的接口(例如,可能使用回调函数)
  • M:N 混合线程,涉及内核和用户模式线程,并可能涉及用户模式线程在操作系统线程之间的迁移。
英文:

A few things distinguish goroutines from typical OS threads:

  • There's user-mode scheduling. When a goroutine is blocked (waiting on the network, say), the Go runtime looks for another one that can run. This happens without having to enter and exit kernel mode and without the OS kernel's scheduler running.
  • There isn't only user-mode scheduling: to use multiple cores, Go will start multiple OS threads and run goroutines on all of them, potentially moving a goroutine between OS threads to keep everything busy. If you hear that goroutines are "multiplexed" onto OS threads, that's what it means.
  • They're meant to have low cost to start. Stacks start out small, only a few kilobytes and growing as needed, whether the OS is using virtual-memory overcommit or not.
  • They're tied in with the language; besides the go statement itself, there are channel types and operations and select statements for coordinating the goroutines.
  • They lack some OS threading features: currently, Go's scheduler doesn't guarantee fairness, and there's only very limited pre-emption (an empty for{} loop will never be switched away from).

They're closely related to lots of other terms:

  • fibers, a term used in connection with user-mode-scheduled threads
  • green threads, another term used to refer to user-mode-scheduled threads
  • coroutines, referring to routines that can yield control to each other at arbitrary points in their code
  • event-driven architectures, which might switch to other tasks while waiting on asynchronous events like network I/O, but may not present a thread-like interface (they might use callback functions instead, for example)
  • M:N hybrid threading, which involves both kernel- and user-mode threads, and can involve migration of user-mode threads across OS threads.

答案2

得分: 1

当有两个CPU时,goroutine会作为真实线程运行。当只有一个CPU时,goroutine会作为单个线程的协程运行,并通过切换上下文来实现。goroutine不会固定在特定的线程上,因此没有像线程ID那样的标识符。如果你想将goroutine固定在操作系统线程上,你需要使用runtime.LockOSThread()

英文:

When having two CPUs, goroutine(s) runs as real threads. When having single CPU, the goroutine(s) runs as co-routine of single thread that running with switching their contexts. goroutine doesn't stick the fixed thread. So it doesn't identifier like thread-id. If you want to stick goroutine as OS thread, you need to use runtime.LockOSThread().

答案3

得分: 0

这篇关于Go调度器的Morsing博客文章很好,因为它有带有三角形、正方形和圆形的图片。

从调度器的角度来看:

goroutine包括堆栈、指令指针和其他对调度很重要的信息。

博客链接:http://morsmachine.dk/go-scheduler

英文:

This Morsing's blog post about the Go scheduler is nice because it has pictures with triangles, squares and circles.

From the scheduler's point of view :

> The goroutine includes the stack, the instruction pointer and other information important for scheduling.

huangapple
  • 本文由 发表于 2015年1月6日 06:47:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/27789227.html
匿名

发表评论

匿名网友

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

确定