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.

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

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页