What is the default goroutine?

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

What is the default goroutine?

问题

以下是要翻译的内容:

以下代码打印出2009/11/10 23:00:00 Num of goroutines: 1。有没有办法知道它是哪个 goroutine?

package main

import (
	"log"
	"runtime"
)

func main() {
	r := runtime.NumGoroutine()
	log.Println("Num of goroutines: ", r)
}

链接:https://play.golang.org/p/pFOG6EQEYb

英文:

The following code prints 2009/11/10 23:00:00 Num of goroutines: 1. Is there a way to know which go routine it is?

package main

import (
	"log"
	"runtime"
)

func main() {
	r := runtime.NumGoroutine()
	log.Println("Num of goroutines: ", r)
}

https://play.golang.org/p/pFOG6EQEYb

答案1

得分: 2

这是主要的goroutine,封装了主线程的执行。例如,在你的示例程序中,你可以通过打印goroutine堆栈跟踪来观察到这一点:

package main

import (
	"log"
	"os"
	"runtime"
	"runtime/pprof"
)

func main() {
	r := runtime.NumGoroutine()
	log.Println("goroutine数量:", r)
	pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
}

运行结果如下:

2009/11/10 23:00:00 goroutine数量: 1
goroutine profile: total 1
1 @ 0xa22a0 0xa1fc0 0x9d640 0x20180 0x5a0a0 0x98e81
#	0xa229f	runtime/pprof.writeRuntimeProfile+0xdf	/usr/local/go/src/runtime/pprof/pprof.go:614
#	0xa1fbf	runtime/pprof.writeGoroutine+0x9f	/usr/local/go/src/runtime/pprof/pprof.go:576
#	0x9d63f	runtime/pprof.(*Profile).WriteTo+0xff	/usr/local/go/src/runtime/pprof/pprof.go:298
#	0x2017f	main.main+0x17f				/tmp/sandbox832735226/main.go:13
#	0x5a09f	runtime.main+0x39f			/usr/local/go/src/runtime/proc.go:183

注意到堆栈跟踪的底部是runtime.main

当程序在转储跟踪时有两个活动的goroutine:

package main

import (
	"log"
	"os"
	"runtime"
	"runtime/pprof"
)

func main() {
	c := make(chan int)
	go func() { <-c }()
	r := runtime.NumGoroutine()
	log.Println("goroutine数量:", r)
	pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
}

运行结果如下:

2009/11/10 23:00:00 goroutine数量: 2
goroutine profile: total 2
1 @ 0x20220 0x98f61
#	0x20220	main.main.func1+0x0	/tmp/sandbox584983760/main.go:12

1 @ 0xa2380 0xa20a0 0x9d720 0x201e0 0x5a180 0x98f61
#	0xa237f	runtime/pprof.writeRuntimeProfile+0xdf	/usr/local/go/src/runtime/pprof/pprof.go:614
#	0xa209f	runtime/pprof.writeGoroutine+0x9f	/usr/local/go/src/runtime/pprof/pprof.go:576
#	0x9d71f	runtime/pprof.(*Profile).WriteTo+0xff	/usr/local/go/src/runtime/pprof/pprof.go:298
#	0x201df	main.main+0x1df				/tmp/sandbox584983760/main.go:15
#	0x5a17f	runtime.main+0x39f			/usr/local/go/src/runtime/proc.go:183
英文:

It's the main goroutine, encapsulating the main thread of execution. You might for instance observe that by printing goroutine stack traces in your example program:

package main

import (
	&quot;log&quot;
	&quot;os&quot;
	&quot;runtime&quot;
	&quot;runtime/pprof&quot;
)

func main() {
	r := runtime.NumGoroutine()
	log.Println(&quot;Num of goroutines: &quot;, r)
	pprof.Lookup(&quot;goroutine&quot;).WriteTo(os.Stdout, 1)
}

Which produces the following:

2009/11/10 23:00:00 Num of goroutines:  1
goroutine profile: total 1
1 @ 0xa22a0 0xa1fc0 0x9d640 0x20180 0x5a0a0 0x98e81
#	0xa229f	runtime/pprof.writeRuntimeProfile+0xdf	/usr/local/go/src/runtime/pprof/pprof.go:614
#	0xa1fbf	runtime/pprof.writeGoroutine+0x9f	/usr/local/go/src/runtime/pprof/pprof.go:576
#	0x9d63f	runtime/pprof.(*Profile).WriteTo+0xff	/usr/local/go/src/runtime/pprof/pprof.go:298
#	0x2017f	main.main+0x17f				/tmp/sandbox832735226/main.go:13
#	0x5a09f	runtime.main+0x39f			/usr/local/go/src/runtime/proc.go:183

Notice that runtime.main at the bottom of the trace.

A program with two goroutines alive when the traces are dumped:

package main

import (
	&quot;log&quot;
	&quot;os&quot;
	&quot;runtime&quot;
	&quot;runtime/pprof&quot;
)

func main() {
	c := make(chan int)
	go func() { &lt;-c }()
	r := runtime.NumGoroutine()
	log.Println(&quot;Num of goroutines: &quot;, r)
	pprof.Lookup(&quot;goroutine&quot;).WriteTo(os.Stdout, 1)
}

Will produce something along the lines of:

2009/11/10 23:00:00 Num of goroutines:  2
goroutine profile: total 2
1 @ 0x20220 0x98f61
#	0x20220	main.main.func1+0x0	/tmp/sandbox584983760/main.go:12

1 @ 0xa2380 0xa20a0 0x9d720 0x201e0 0x5a180 0x98f61
#	0xa237f	runtime/pprof.writeRuntimeProfile+0xdf	/usr/local/go/src/runtime/pprof/pprof.go:614
#	0xa209f	runtime/pprof.writeGoroutine+0x9f	/usr/local/go/src/runtime/pprof/pprof.go:576
#	0x9d71f	runtime/pprof.(*Profile).WriteTo+0xff	/usr/local/go/src/runtime/pprof/pprof.go:298
#	0x201df	main.main+0x1df				/tmp/sandbox584983760/main.go:15
#	0x5a17f	runtime.main+0x39f			/usr/local/go/src/runtime/proc.go:183

huangapple
  • 本文由 发表于 2017年1月24日 09:30:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/41818599.html
匿名

发表评论

匿名网友

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

确定