主函数是作为goroutine运行的吗? [英] Is the main function runs as a goroutine?

查看:84
本文介绍了主函数是作为goroutine运行的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

main()函数是否作为goroutine运行?例如,我看到了如下的崩溃堆栈跟踪,这让我发问:

Is the main() function runs as a goroutine? For example, I've seen a crash stack trace like the below, which makes me ask:

goroutine 1 [running]: main.binarySearch(0x0, 0x61, 0x43,
0xc420043e70, 0x19, 0x19, 0x10)
     /home/---/go/src/github.com/----/sumnum.go:22 +0x80 main.main()
     /home/---/go/src/github.com/---/sumnum.go:13 +0xc1 exit status 2

推荐答案

是的,主要功能作为goroutine(主要功能)运行.

Yes, the main function runs as a goroutine (the main one).

根据 https://tour.golang.org/concurrency/1

goroutine是Go运行时管理的轻量级线程. go f(x, y, z) 启动一个运行f(x, y, z)的新goroutine.f,x,y和z的求值发生在当前goroutine 中,而f的执行发生在新的goroutine中.
Goroutine在相同的地址空间中运行,因此必须同步访问共享内存.同步包提供了有用的原语,尽管您在Go中不需要太多,因为还有其他原语.

A goroutine is a lightweight thread managed by the Go runtime. go f(x, y, z) starts a new goroutine running f(x, y, z) The evaluation of f, x, y, and z happens in the current goroutine and the execution of f happens in the new goroutine.
Goroutines run in the same address space, so access to shared memory must be synchronized. The sync package provides useful primitives, although you won't need them much in Go as there are other primitives.

因此,根据此官方文档,main当前goroutine .
确切地说(从字面上看),我们可以将main作为当前的goroutine来解决,因此简单地说就是goroutine. (注意:从字面上讲main()是可以作为goroutine运行的函数.)

So according to this official document the main is the current goroutine.
To be precise (literally) we could address the main as the current goroutine, so simply speaking it is a goroutine. (Note: Literally speaking the main() is a function which could run as a goroutine.)

现在让我们使用runtime.NumGoroutine() 计数个goroutine的数量:

Now let's count the number of goroutines using runtime.NumGoroutine():

作为示例,我们运行3个goroutine.试试在线:

As an example let's run 3 goroutines. Try it online:

package main

import (
    "fmt"
    "runtime"
    "time"
)

func main() {
    fmt.Println(runtime.NumGoroutine()) // 3
    time.Sleep(100 * time.Millisecond)
}
func init() {
    go main()
    go main()
}


这里当前goroutine 运行新goroutine ,因此这里我们有个以上goroutine ,它们会再次执行main().试试在线:


Here the current goroutine runs the new goroutine, so here we have more than one goroutine, which executes main() again. Try it online:

package main

import (
    "fmt"
    "runtime"
    "sync/atomic"
    "time"
)

func main() {
    fmt.Println(runtime.NumGoroutine()) // 1 2 3 4
    if atomic.LoadInt32(&i) <= 0 {
        return
    }
    atomic.AddInt32(&i, -1)
    go main()
    time.Sleep(100 * time.Millisecond)
}

var i int32 = 3

输出:

1
2
3
4

这里我们有一个main goroutine加上3个名为main goroutines的用户,因此这里的goroutine总数为4.

Here we have one main goroutine plus 3 user called main goroutines, so total number of goroutines are 4 here.

让我们使用main()(一个goroutine -无需同步)来计算阶乘.试试在线:

Let's calculate factorial using main() (one goroutine - no synchronization needed). Try it online:

package main

import "fmt"

func main() {
    if f <= 0 {
        fmt.Println(acc)
        return
    }
    acc *= f
    f--
    main()
}

var f = 5
var acc = 1

输出:

120

注意:上面的代码仅用于清楚地表达我的观点,并不适合生产使用(使用全局变量不应该是首选).

Note: The codes above are just for clearly showing my viewpoints and is not good for production use (Using global variables should not be the first choice).

这篇关于主函数是作为goroutine运行的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆