dqls67891 2018-12-26 08:46
浏览 117
已采纳

是什么导致空的Go for循环锁定程序?

As I was learning Go I initially wrote my short programs using an empty for loop to block the program from exiting as I ran my test functions with the go keyword. As my test/learning programs grew in size however, the entire program would sometimes freeze at random places and the debugger would disconnect, making debugging really hard.

I eventually learned that the cause was the empty for loop from a bit of discussion on the IRC, and replaced it with a blocking channel, but I never learned WHY other than it was something to do with how Go handles scheduling.

What mechanism in the background causes independent go-routines to lock entire programs, even when there are plenty of cores allocated to the program, if there is an empty infinite for loop?

  • 写回答

2条回答 默认 最新

  • dqitk20644 2018-12-26 09:20
    关注

    For example, Issue #10958: runtime: tight loops should be preemptible #10958

    Currently goroutines are only preemptible at function call points. Hence, it's possible to write a tight loop (e.g., a numerical kernel or a spin on an atomic) with no calls or allocation that arbitrarily delays preemption. This can result in arbitrarily long pause times as the GC waits for all goroutines to stop.

    In unusual situations, this can even lead to deadlock when trying to stop the world. For example, the runtime's TestGoroutineParallelism tries to prevent GC during the test to avoid deadlock. It runs several goroutines in tight loops that communicate through a shared atomic variable. If the coordinator that starts these is paused part way through, it will deadlock.

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

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题