Golang线程计数误导性

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

golang thread count misleading

问题

我在Go语言上编写了一个小应用程序,它启动了4个线程来执行各种任务,还有一个主线程。所以总共有5个线程。但是,如果我启动活动监视器并监视进程,我看到的情况是这样的:

首先,为什么有7个线程?而且它不是固定的。有时是5个,有时是7个。而且,由主线程启动的所有4个线程在完成任务后都会结束。我通过在线程顶部放置一个不同的语句来验证线程的结束。但是活动监视器中的线程计数仍然是7。

有人知道这里发生了什么吗?这些额外的线程是由Go运行时启动的吗?有没有办法找出由我的代码启动而不是由Go运行时启动的活动线程数。

英文:

I have written a small application on go, which starts 4 threads for doing various things + one main thread. So in total there are 5 threads. But if I'll start activity monitor and monitor the process, this is what I see

Golang线程计数误导性

First of all why 7 threads. And it is not constant. Sometimes it is 5 and other times it is 7. Also all 4 threads started by main thread ends after doing hat they are suppose to. I verify that threads end by putting a differ statement on the top of thread. Still thread count in Activity monitor stays 7.

Does anyone knows what is going on over here? Are these extra threads started by go runtime? Is there a way to find out how many threads are active my program that are started by my code and not by go runtime.

答案1

得分: 8

是的,它们由运行时启动,例如http://play.golang.org/p/c0cIngo_sO,它将打印出正在运行的4个goroutine。

Goroutine不是线程,一个操作系统线程可以处理数百个goroutine,但是如果你正在执行一些繁重的操作或使用阻塞的系统调用,运行时会启动一个新的线程来处理其他的goroutine。

英文:

Yes they are started by the runtime, for example http://play.golang.org/p/c0cIngo_sO it will print 4 goroutines are running.

Goroutines aren't threads, 1 OS thread can handle 100s of goroutines, however if you're doing something heavy or using a blocking system call, the runtime will start a new thread to handle the other goroutines.

答案2

得分: 3

我猜你说的线程是指 Goroutines。

Go 运行时会将轻量级的 Goroutines 透明地复用到操作系统的线程上。这也是为什么你不需要调用像 select() 这样的函数,这是运行时的工作。

如果你创建了 7 个 Goroutines,并且其中一些被阻塞,运行时可能会决定终止空闲的操作系统线程。这就是为什么你看到的线程数少于 Goroutines 数量的原因。

英文:

I suppose you mean Goroutines when you say threads.

The Go runtime transparently multiplexes lightweight Goroutines onto OS threads. That's also why you don't need to call functions like select()—that's the runtime's job.

If you spawn 7 Go routines and some of them block, the runtime might decide to terminate the idle OS threads. This is why you see less threads than Go routines.

答案3

得分: 1

我认为你把Goroutines误认为线程了。

在你的Go程序中,你所指的线程实际上是Goroutine,它是一种协程,不是真正的线程,它是由Go的运行时实现的(你需要了解Go运行时,每个Go程序都在运行时上运行,运行时实际上使用线程来实现Goroutines)。不同的Goroutine可能在同一个线程中运行,也可能不在同一个线程中运行,但你永远不知道。你可以使用runtime.GOMAXPROCS来适应多核CPU。

而你在监视器中看到的线程是真正的线程。

英文:

I think you mistake Goroutines for thread.

In your go program, the thread you mean is actually goroutine ,which is a coroutine and is not a real thread , which is implemented by go's runtime(you need to know about go runtime, every go program is running on a runtime, and runtime actually use thread to implement goroutines).Diffrent goroutine may be running in the same thread, or may be not ,but you never know . You can use runtime.GOMAXPROCS for multi-core cpu .

And the threads you see in the monitor are real threads .

huangapple
  • 本文由 发表于 2014年7月30日 05:58:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/25025662.html
匿名

发表评论

匿名网友

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

确定