duanche4578 2014-08-01 06:03
浏览 127
已采纳

Golang:Goroutine无限循环

When an fmt.Print() line is removed from the code below, code runs infinitely. Why?

package main

import "fmt"
import "time"
import "sync/atomic"

func main() {
        var ops uint64 = 0 
        for i := 0; i < 50; i++ {
                go func() {
                        for {
                                atomic.AddUint64(&ops, 1)
                                fmt.Print()
                        }
                }()
        }
        time.Sleep(time.Second)
        opsFinal := atomic.LoadUint64(&ops)
        fmt.Println("ops:", opsFinal)
}
  • 写回答

1条回答 默认 最新

  • dqkf49487 2014-08-01 06:13
    关注

    The Go By Example article includes:

       // Allow other goroutines to proceed.
       runtime.Gosched()
    

    The fmt.Print() plays a similar role, and allows the main() to have a chance to proceed.

    A export GOMAXPROCS=2 might help the program to finish even in the case of an infinite loop, as explained in "golang: goroute with select doesn't stop unless I added a fmt.Print()".

    fmt.Print() explicitly passes control to some syscall stuff


    Yes, go1.2+ has pre-emption in the scheduler

    In prior releases, a goroutine that was looping forever could starve out other goroutines on the same thread, a serious problem when GOMAXPROCS provided only one user thread.

    In Go 1.2, this is partially addressed: The scheduler is invoked occasionally upon entry to a function. This means that any loop that includes a (non-inlined) function call can be pre-empted, allowing other goroutines to run on the same thread.

    Notice the emphasis (that I put): it is possible that in your example the for loop atomic.AddUint64(&ops, 1) is inlined. No pre-emption there.


    Update 2017: Go 1.10 will get rid of GOMAXPROCS.

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化