小编典典

Google Go Goroutine的中断模式(速度问题)

go

我运行一个goroutine来增加一个计数器,该计数器可以被命令行输入“ t \ n”打断

在该select语句中,如果我选择使用default:,则计数器变量会j飞到前言。对我来说这很正常。

但是,如果我选择使用case <-time.After(100*time.Microsecond):,则计数器j在一秒内最多只能增加60个左右,而不是10,000个。

实际上,无论我输入什么值time.After(),在select声明中我只能得到大约60Hz的频率。

为什么?

package main
import (
    "bufio"
    "fmt"
    "os"
    "strings"
    "time"
)

func main() {
    message := make(chan string)
    go check_input(message)
    work_loop(message)
}

func work_loop(message chan string) {
    //var j [][]int
    var j int
    t0:=time.Now()
Loop:
    for {
        select {
        case msg := <-message:
            if msg == "terminate" {
                //fmt.Println("end task")
                t1:=time.Now()
                fmt.Println(j)
                fmt.Println("total duration:", t1.Sub(t0))
                break Loop
            }
        case <-time.After(100 * time.Microsecond):
        //default:
            //do work here          
            j += 1
            fmt.Println(j)
            break

        }
    }
    //fmt.Println("exit work loop")
}

func check_input(msg chan string) {
    reader := bufio.NewReader(os.Stdin)
    for {
        line, err := reader.ReadString('\n')

        if err != nil {
            // You may check here if err == io.EOF
            break
        }

        if strings.TrimSpace(line) == "t" {
            msg <- "terminate"
        }
    }
}

阅读 397

收藏
2020-07-02

共1个答案

小编典典

它与的精度有关time.Timer。查看以下文档time.After

[…]等效于NewTimer(d).C。

以及以下文件time.NewTimer

NewTimer创建一个新的Timer,它将 至少在 持续时间d 之后 其通道上发送当前时间。

(强调我的)

这样做的原因是NewTimer委托给运行时(与OS相关)计时器,使此计时器的行为取决于基础OS(以及Go集成的实现)。

通常,根据我的经验,亚毫秒级粒度在任何语言中都没有良好的跨平台支持, 尤其是 在Windows XP上。

2020-07-02