Go程序中的三个背景程序是什么? [英] What are the three background goroutines in a Go program?
问题描述
http://play.golang.org/p/MQBiLmHXBK
包主
import(
fmt
runtime
)
func main(){
fmt.Println(runtime.NumGoroutine ))// 4
}
那些不是线程,它们是goroutines,并且数量可能会根据当前的实现而有所不同(例如,在go1.2中它会打印2)。
现在,它看起来像 main
和1 for runtime / gc。
func main(){
fmt.Println(func()string {panic(nil); return}())
}
这显示
goroutine 16 [running]:
runtime.panic(0x0,0x0)
/usr/local/go/src/pkg/runtime/panic.c:279 + 0xf5
main.func·001(0x3ea4f,0xdc4b0)
/test/threads.go:6+ 0x28
main.main()
/test/threads.go:6+ 0x1e
goroutine 17 [runnable]:
runtime.MHeap_Scavenger()
/usr/local/go/src/pkg/runtime/mheap.c:507
runtime .goexit()
/usr/local/go/src/pkg/runtime/proc.c:1445
goroutine 18 [runnable]:
bgsweep()
/usr/local/go/src/pkg/runtime/mgc0.c:1976
runtime.goexit()
/usr/local/go/src/pkg/runtime/proc.c:1445
goroutine 19 [runnable]:
runfinq()
/usr/local/go/src/pkg/runtime/mgc0.c:2606
runtime.goexit ()
/usr/local/go/src/pkg/runtime/proc.c:1445
如果您删除fmt,并使用自引导 print
函数,那么您只能获得2个goroutine。
$ $ b $ print(runtime.NumGoroutine(),\\\
)
//打印2
如果您想要确切知道哪些goroutines正在运行,打印堆栈跟踪,调用panic或使用SIGQUIT(打印堆栈跟踪并退出)终止进程。如果你运行绝对最低限度的程序,你可以看到一个堆栈跟踪,你可以看到2个例程:
package main
func main(){
panic(nil)
}
< Goroutines非常便宜,许多事情将会启动并停止更多goroutines,因此试图追踪它们的下限并不是很有用。注意,即使只有2个goroutines(main / runtime.panic和runtime.MHeap_Scavenger),count也已经达到了17个。
panic:nil
goroutine 16 [running]:
runtime.panic(0x0,0x0)
/ usr / local / go / src / pkg / runtime / panic.c:279 + 0xf5
main.main()
/test/threads.go:4+ 0x28
goroutine 17 [runnable]:
runtime。 MHeap_Scavenger()
/usr/local/go/src/pkg/runtime/mheap.c:507
runtime.goexit()
/ usr / local / go / src / pkg / runtime /proc.c:1445
退出状态2
Go seems to always have at least 4 goroutines running at any given time. What are the other three that are not the main goroutine?
http://play.golang.org/p/MQBiLmHXBK
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Println(runtime.NumGoroutine()) //4
}
Those aren't threads, they're goroutines, and the number may vary based on current implementation (i.e. in go1.2 it would have printed 2).
Right now, it looks like you have 1 for main
, and 3 for runtime/gc.
import "fmt"
func main() {
fmt.Println(func() string { panic(nil); return "" }())
}
This shows
goroutine 16 [running]:
runtime.panic(0x0, 0x0)
/usr/local/go/src/pkg/runtime/panic.c:279 +0xf5
main.func·001(0x3ea4f, 0xdc4b0)
/test/threads.go:6 +0x28
main.main()
/test/threads.go:6 +0x1e
goroutine 17 [runnable]:
runtime.MHeap_Scavenger()
/usr/local/go/src/pkg/runtime/mheap.c:507
runtime.goexit()
/usr/local/go/src/pkg/runtime/proc.c:1445
goroutine 18 [runnable]:
bgsweep()
/usr/local/go/src/pkg/runtime/mgc0.c:1976
runtime.goexit()
/usr/local/go/src/pkg/runtime/proc.c:1445
goroutine 19 [runnable]:
runfinq()
/usr/local/go/src/pkg/runtime/mgc0.c:2606
runtime.goexit()
/usr/local/go/src/pkg/runtime/proc.c:1445
if you remove fmt, and use the bootstrapping print
function you only get 2 goroutines.
import "runtime"
func main() {
print(runtime.NumGoroutine(), "\n")
}
// prints 2
If you ever want to know exactly what goroutines are running, print a stack trace, call panic, or kill the process with SIGQUIT (which prints a stack trace and exits). If you run the absolute minimum program you can get a stack trace from you can see the 2 goroutines:
package main
func main() {
panic(nil)
}
Goroutines are very inexpensive, and many things will start and stop more goroutines, so trying to track their lower bound isn't very useful. Notice how even though there's only 2 goroutines, (main/runtime.panic, and runtime.MHeap_Scavenger), the count is already up to 17.
panic: nil
goroutine 16 [running]:
runtime.panic(0x0, 0x0)
/usr/local/go/src/pkg/runtime/panic.c:279 +0xf5
main.main()
/test/threads.go:4 +0x28
goroutine 17 [runnable]:
runtime.MHeap_Scavenger()
/usr/local/go/src/pkg/runtime/mheap.c:507
runtime.goexit()
/usr/local/go/src/pkg/runtime/proc.c:1445
exit status 2
这篇关于Go程序中的三个背景程序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!