duandao2083 2013-09-12 18: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 19: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.

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

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择