如何杀死goroutines? [英] How to kill goroutines?
本文介绍了如何杀死goroutines?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何杀死/停止goroutines.所有示例均基于频道和选择,这似乎仅在goroutine包含一些可以在频道上收听的重复任务时才有效.有没有办法在下面的goroutine返回之前停止它?
I would like to know how to kill/stop goroutines. All examples are based on channels and select, which seems to only work if the goroutine contains some repeating task between which it can listen on a channel. Is there a way to stop the below goroutine before it returns?
package main
import (
"time"
)
func main() {
stop := make(chan string, 1)
go func() {
time.Sleep(10 * time.Second)
stop <- "stop"
return
}()
<- stop
}
推荐答案
是否可以在返回以下goroutine之前停止它?
Is there a way to stop the below goroutine before it returns?
没有(除了调用os.Exit终止整个程序外).
No there is not (except calling os.Exit to abort the whole program).
Goroutines是独立的,无法从外部控制.
Goroutines are self-contained and not controllable from the outside.
这篇关于如何杀死goroutines?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文