-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
go func() 是如何新建Goroutine的? #6
Comments
go func() 是如何新建Goroutine的?下列程序中, package main
func sum(a, b int) int {
return a + b
}
func main() {
v1 := 1
v2 := 2
go sum(v1, v2)
} 分析使用命令生成汇编代码: 移除部分无效代码后,main函数对应的Go汇编如下,
从main函数进入初始化,到使用go func()创建协程,main函数的栈如下:
使用 |
测试环境为Go1.5.4版本: go version
go version go1.5.4 linux/amd64
func newproc(siz int32, fn *funcval) {
argp := add(unsafe.Pointer(&fn), ptrSize)
pc := getcallerpc(unsafe.Pointer(&siz))
systemstack(func() {
newproc1(fn, (*uint8)(argp), siz, 0, pc)
})
} 该函数的处理逻辑主要在runtime.newproc1函数,其主要作用:
具体流程如下图, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
下列程序中,
go func()
背后发生了什么?The text was updated successfully, but these errors were encountered: