dro62273 2019-04-11 12:09
浏览 25

为什么select {} vs select {case}导致不同的调度

1.select {case}

    "fmt"
    "runtime"
    "time"
)

func main() {
    runtime.GOMAXPROCS(1)

    go func() {
        fmt.Println("a")
    }()
    go func() {
        fmt.Println("b")
    }()
    go func() {
        fmt.Println("c")
    }()

    fmt.Println("d")

    select {
    case _ = <-time.After(time.Second):
    }
}

result:dabc

2.select {}

package main
import (
    "fmt"
    "runtime"

)
func main() {
    runtime.GOMAXPROCS(1)

    go func() {
        fmt.Printf("a")

    }()

    go func() {
        fmt.Printf("b")

    }()

    go func() {
        fmt.Printf("c")

    }()

    fmt.Printf("d")
    select {}

}

result:dcab

Because I set "runtime.GOMAXPROCS(1)", there's only one "Processor" in my program.

One "Processor" has only one "runq", and in/out operation is "FIFO"

My question is "why always... not rand...."

  • 写回答

1条回答 默认 最新

  • dsfdsf21312 2019-04-12 02:43
    关注

    Finally figured it out.

    Processor.runq is FIFO, there's another variable(runnext) here, runnext saves the lastest goroutine which where will be processed next.

    The latest goroutine kicks p.runnext

    Timer creates a goroutine ,and this goroutine kicks p.runnext.


    Processor的runnext 被定时器创建的goroutine kick调, 导致下一个执行的是定时器的goroutine,然后其他的FIFO

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据