V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
cxyfreedom
V2EX  ›  问与答

关于 goroutine 调度问题

  •  1
     
  •   cxyfreedom · Jan 9, 2019 · 787 views
    This topic created in 2671 days ago, the information mentioned may be changed or developed.

    代码如下

    package main
    
    import (
    	"fmt"
    	"runtime"
    	"sync"
    )
    
    func main() {
    	runtime.GOMAXPROCS(1)
        var wg sync.WaitGroup
        wg.Add(3)
        go func() {
            fmt.Println("a")
            wg.Done()
        }()
        go func() {
            fmt.Println("b")
            wg.Done()
        }()
    	go func() {
            fmt.Println("c")
            wg.Done()
        }()
        wg.Wait()
    }
    

    输出结果

    c
    a
    b
    

    事先了解过 GPM 的模型,GOMAXPROCS 大于 1 的情况乱序这个了解。现在将利用的核数设置为 1,按 goroutine 加入 queue 的顺序来说以及代码中逻辑执行时间来说应该也是顺序的,但是实际情况总是最后一个 goroutine 先执行。想问一下,单核情况下,goroutine 的调度到底是怎么样的?

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   812 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:37 · PVG 03:37 · LAX 12:37 · JFK 15:37
    ♥ Do have faith in what you're doing.