duandao2083 2013-09-12 10:46
浏览 19
已采纳

如何阻止程序/ goroutine?

I have a program that fires off two goroutines that provide services in the background. I then want to block the main goroutine and let them run in the background. I want to block 'forever' and I don't care about clean exits. How should I do this? I could wait on channel and then never send anything down it. I could sleep in a loop. Neither feels quite right I thought there might be a simpler block() function I could call?

I'm currently doing this

var i chan int
<-i
  • 写回答

1条回答 默认 最新

  • duanlang1531 2013-09-12 11:09
    关注

    You can use a sync.WaitGroup which you pass to each of your goroutine. This is the common way to wait in the calling goroutine for its children.

    However, in your case where you don't care for the results this should do as well:

    select {}
    

    From the spec regarding select:

    If there are no cases with non-nil channels, the statement blocks forever

    This statement blocks forever while yielding control to the other goroutines.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部