可以使用活动的goroutine退出程序吗?

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

OK to exit program with active goroutine?

问题

以下是代码的翻译:

func main() {
    ch := make(chan int)
    quit := make(chan int)
    go func() {
        for {
            ch <- querySomePeriodicThing()
        }
    }()

    // ...

loop:
    for {
        select {
        case <-ch:
            handlePeriodicThing()
        case <-quit:
            break loop
        }
    }
}

这段代码中的goroutine应该在整个程序执行期间一直运行。当select语句从quit通道接收到内容时,它会跳出循环并结束程序,而不会尝试停止goroutine。

我的问题是:这样做会有任何间歇性的不明显的负面影响吗?我知道在其他语言中,线程应该在程序结束之前进行清理(即退出),但Go语言是否不同?假设querySomePeriodicThing()不会打开文件描述符、套接字或任何可能导致不良后果的东西。

英文:

Take the following code snippet:

func main() {
	ch := make(chan int)
	quit := make(chan int)
	go func() {
		for {
			ch &lt;- querySomePeriodicThing()
		}
	}()

	// ...

loop:
	for {
		select {
		case &lt;-ch: handlePeriodicThing()
		case &lt;-quit: break loop
		}
	}
}

The goroutine should run for the duration of execution. When the select statement receives something from the quit channel, it breaks out of the loop and the program ends, without any attempt to stop the goroutine.

My question: will this have any intermittent adverse effects that are not obvious from running it once or twice? I know that in other languages threads should be cleaned up (i.e., exited) before the program ends, but is go different? Assume that querySomePeriodicThing() does not open file descriptors or sockets or anything that would be bad to leave open.

答案1

得分: 37

根据规范中提到的,当main函数完成时,你的程序将退出:

程序的执行从初始化主包开始,然后调用main函数。当该函数调用返回时,程序退出。它不会等待其他(非main)goroutine 完成。

因此,从语言的角度来看,你仍然有其他的goroutine在运行并不是一个问题。根据你的程序在做什么,这可能仍然是一个问题。

如果goroutine在程序退出之前创建了一些需要清理的资源,那么在执行过程中停止可能会成为一个问题:在这种情况下,你应该让main函数先等待它们完成。Go语言中没有类似于pthread_join的等待函数,所以你需要自己编写代码来实现这一点(例如,使用通道或sync.WaitGroup)。

需要注意的是,某些资源在进程退出时会被操作系统自动清理(例如打开的文件、文件锁等),因此在某些情况下不需要特殊的清理操作。

英文:

As mentioned in the spec, your program will exit when the main function completes:

> Program execution begins by initializing the main package and then invoking the function main. When that function invocation returns, the program exits. It does not wait for other (non-main) goroutines to complete.

So the fact you have other goroutines still running is not a problem from a language point of view. It may still be a problem depending on what your program is doing.

If the goroutine has created some resources that should be cleaned up before program exit, then having execution stop mid-way through might be a problem: in this case, you should make your main function wait for them to complete first. There is no equivalent to pthread_join, so you will need to code this yourself (e.g. by using a channel or sync.WaitGroup).

Note that for some resources are automatically cleaned up by the operating system on process exit (e.g. open files, file locks, etc), so in some cases no special clean up will be necessary

答案2

得分: -2

Goroutines不是线程,它们非常轻量级,当它们不再运行或程序退出时,运行时会自动清理它们。

英文:

Goroutines aren't threads, they are very lightweight and the runtime automatically cleans them up when they are no longer running, or if the program exits.

huangapple
  • 本文由 发表于 2014年8月27日 11:34:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/25518531.html
匿名

发表评论

匿名网友

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

确定