dqwcdqs358367 2013-03-12 21:57
浏览 49
已采纳

通知所有goroutine

I am working on a TCP Server in Go. Now I want to notify all goroutines that are talking to clients to drop their connections, dump what they've got and stop.

Closing a channel is a way to notify all of them.

Question is: Is that idiomatic Go? If I am wrong; then what should I do (for notifying all of goroutines - something like ManualResetEvent in .NET)?

Note: I am a Go newbie, just learning and started with TCP Server because I have written that before in C#.

  • 写回答

2条回答 默认 最新

  • dsdsm2016 2013-03-12 22:04
    关注

    Yes, closing a channel is an idiomatic Go way of communicating between Goroutines.

    You'll need to pass a channel into each goroutine as it launches and check the channel with the select call after each network event.

    You'll also want to set timeouts on network events so that you don't have connections hanging around forever.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接