Go程序中的操作系统线程

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

OS Threads in a Go Program

问题

我正在使用go 1.3.3版本。
我试图理解Go何时生成新线程。我有以下两个Go程序:

程序1:

package main

func main() {
     for ;; {
     }
}

程序2:

package main
import (
         "sync"
)

func justrun(wg *sync.WaitGroup) {
     for ;; {
     }
     wg.Done()
}

func main() {
     var wg sync.WaitGroup
     go justrun(&wg)
     wg.Wait()
}

在活动监视器中,我看到程序1有2个线程,程序2有3个线程。我有多个问题:

  1. 在程序1中,这2个线程是什么。我猜一个是"main",但另一个是什么?
  2. 在程序2中,一旦我引入wg.Wait(),线程的数量变为3。我预测程序2应该使用与程序1相同数量的线程。我的理解是,假设main()在上下文P1的线程M1上运行。goroutine将进入P1的运行队列。
    我在某个地方读到,sync包中的所有原语在被阻塞时不会导致goroutine使用线程。如果是这样的话,goroutine应该与主函数在P1上下文和M1上并发运行,不需要新线程。wg.Wait()是一个线程阻塞调用吗?

感谢任何帮助。

英文:

I am using go 1.3.3.
I am trying to understand when Go spawns new threads. I have the following two Go programs:

Program 1:

package main

func main() {
     for ;; {
     }
}

Program 2:

package main
import (
         "sync"
)

func justrun(wg *sync.WaitGroup) {
     for ;; {
     }
     wg.Done()
}

func main() {
     var wg sync.WaitGroup
     go justrun(&wg)
     wg.Wait()
}

In the activity Monitor I see that for Program 1 there are 2 threads and for Program 2 there are 3 threads. I have multiple questions:

  1. In Program 1 what are these 2 threads. I guess one is for "main", but what is the other one for?
  2. In Program 2, once i introduce wg.Wait() the number of threads became 3. I was predicting Program 2 to use the same number of threads as Program 1. My understanding is that assume that main() is running on a thread M1 with context P1. The goroutine will go into the runqueue of P1.
    I read somewhere that all primitives in sync package do not cause the goroutine to use a thread when it gets blocked because of them. If this is the case, then goroutine should run concurrently with main in P1 context and on M1 and there should be no need of a new thread. Is wg.Wait() a thread blocking call?

Any help is appreciated.

答案1

得分: 2

规则是go将为用户级别代码生成确切的GOMAXPROCS线程。根据runtime文档:

GOMAXPROCS变量限制了可以同时执行用户级别Go代码的操作系统线程的数量。

但请注意:

在代表Go代码进行系统调用的情况下,可以阻塞的线程数量没有限制;它们不计入GOMAXPROCS限制。

其余部分由运行时决定,这取决于具体的实现(和版本)。可能有更多的goroutine在运行,你可能没有意识到;请参考https://stackoverflow.com/questions/25747970/what-are-the-three-background-goroutines-in-a-go-program

英文:

The rules are that go will spawn exactly GOMAXPROCS threads for user-level code. From the runtime documentation:

> The GOMAXPROCS variable limits the number of operating system threads
> that can execute user-level Go code simultaneously.

But note:

> There is no limit to the number of threads that can be blocked in
> system calls on behalf of Go code; those do not count against the
> GOMAXPROCS limit.

The rest is up to the runtime, and is implementation (and version) specific. There are also probably more goroutines running than you may realize; see https://stackoverflow.com/questions/25747970/what-are-the-three-background-goroutines-in-a-go-program

huangapple
  • 本文由 发表于 2014年10月17日 19:38:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/26424224.html
匿名

发表评论

匿名网友

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

确定