duanmaifu3428 2018-09-27 05:38
浏览 207

在go中使用sleep时执行哪个goroutine?

i am new in golang recently. i have a question about goroutine when use time.sleep function. here the code.

package main

import (
    "fmt"
    "time"
)

func go1(msg_chan chan string) {
    for {
        msg_chan <- "go1"
    }
}
func go2(msg_chan chan string) {
    for {
        msg_chan <- "go2"
    }
}
func count(msg_chan chan string) {
    for {
        msg := <-msg_chan
        fmt.Println(msg)
        time.Sleep(time.Second * 1)
    }
}
func main() {
    var c chan string
    c = make(chan string)
    go go1(c)
    go go2(c)
    go count(c)
    var input string
    fmt.Scanln(&input)
}

and output is

go1
go2
go1
go2
go1
go2

i think when count function is execute sleep function, go1 and go2 will execute in random sequence. so the out put maybe like

go1
go1
go2
go2
go2
go1

when i delete the sleep code in count function. the result as i supposed , it's random. i am stucked in this issue. thanks.

  • 写回答

1条回答 默认 最新

  • dongye6377 2018-09-27 06:24
    关注

    First thing to notice is that there are three go routines and all of them are independent of each other. The only thing that combines the two go routines with count routine is the channel on which both go routines are sending the values.

    time.Sleep is not making the go routines synchronous. On using time.Sleep you are actually letting the count go routine to wait for that long which let other go routine to send the value on the channel which is available for the count go routine to be able to receive.

    One more thing that you can do to check it is increase the number of CPU's which will give you the random result.

    func GOMAXPROCS(n int) int
    

    GOMAXPROCS sets the maximum number of CPUs that can be executing simultaneously and returns the previous setting. If n < 1, it does not change the current setting. The number of logical CPUs on the local machine can be queried with NumCPU. This call will go away when the scheduler improves.

    The number of CPUs available simultaneously to executing goroutines is controlled by the GOMAXPROCS shell environment variable, whose default value is the number of CPU cores available. Programs with the potential for parallel execution should therefore achieve it by default on a multiple-CPU machine. To change the number of parallel CPUs to use, set the environment variable or use the similarly-named function of the runtime package to configure the run-time support to utilize a different number of threads. Setting it to 1 eliminates the possibility of true parallelism, forcing independent goroutines to take turns executing.

    Considering the part where output of the go routine is random, it is always random. But channels most probably work in queue which is FIFO(first in first out) as it depends on which value is available on the channel to b received. So whichever be the value available on the channel to be sent is letting the count go routine to wait and print that value.

    Take for an example even if I am using time.Sleep the output is random:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func go1(msg_chan chan string) {
        for i := 0; i < 10; i++ {
            msg_chan <- fmt.Sprintf("%s%d", "go1:", i)
        }
    }
    func go2(msg_chan chan string) {
        for i := 0; i < 10; i++ {
            msg_chan <- fmt.Sprintf("%s%d", "go2:", i)
        }
    }
    func count(msg_chan chan string) {
        for {
            msg := <-msg_chan
            fmt.Println(msg)
            time.Sleep(time.Second * 1)
        }
    }
    func main() {
        var c chan string
        c = make(chan string)
        go go1(c)
        go go2(c)
        go count(c)
        time.Sleep(time.Second * 20)
        fmt.Println("finished")
    }
    

    This sometimes leads to race condition which is why we use synchronization either using channels or wait.groups.

    package main
    
    import (
        "fmt"
        "sync"
        "time"
    )
    
    var wg sync.WaitGroup
    
    func go1(msg_chan chan string) {
        defer wg.Done()
        for {
            msg_chan <- "go1"
        }
    }
    func go2(msg_chan chan string) {
        defer wg.Done()
        for {
            msg_chan <- "go2"
        }
    }
    func count(msg_chan chan string) {
        defer wg.Done()
        for {
            msg := <-msg_chan
            fmt.Println(msg)
            time.Sleep(time.Second * 1)
        }
    }
    func main() {
        var c chan string
        c = make(chan string)
        wg.Add(1)
        go go1(c)
        wg.Add(1)
        go go2(c)
        wg.Add(1)
        go count(c)
        wg.Wait()
        fmt.Println("finished")
    }
    

    Now coming to the part where you are using never ending for loop to send the values on a channel. So if you remove the time.Sleep your process will hang since the loop will never stop to send the values on the channel.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向