1

Go ではtime.After、スリープ状態の関数をタイムアウトにするために使用できますが、ビジー待機中 (または動作中) の関数に対して同じことを行うことはできません。次のコードはtimed out、1 秒後に戻り、その後ハングします。

package main

import (
        "fmt"
        "time"
)

func main() {
        sleepChan := make(chan int)
        go sleep(sleepChan)
        select {
        case sleepResult := <-sleepChan:
                fmt.Println(sleepResult)
        case <-time.After(time.Second):
                fmt.Println("timed out")
        }

        busyChan := make(chan int)
        go busyWait(busyChan)
        select {
        case busyResult := <-busyChan:
                fmt.Println(busyResult)
        case <-time.After(time.Second):
                fmt.Println("timed out")
        }
}

func sleep(c chan<- int) {
        time.Sleep(10 * time.Second)
        c <- 0
}

func busyWait(c chan<- int) {
        for {
        }
        c <- 0
}

2 番目のケースでタイムアウトが発生しないのはなぜですか? また、作業中のゴルーチンを中断するためにどのような代替手段を使用する必要がありますか?

4

1 に答える 1

6

for {}ステートメントは、単一のプロセッサを独占する無限ループです。runtime.GOMAXPROCSタイマーを実行できるようにするには、2 以上に設定します。

例えば、

package main

import (
    "fmt"
    "runtime"
    "time"
)

func main() {
    fmt.Println(runtime.GOMAXPROCS(0))
    runtime.GOMAXPROCS(runtime.NumCPU())
    fmt.Println(runtime.GOMAXPROCS(0))
    sleepChan := make(chan int)
    go sleep(sleepChan)
    select {
    case sleepResult := <-sleepChan:
        fmt.Println(sleepResult)
    case <-time.After(time.Second):
        fmt.Println("timed out")
    }

    busyChan := make(chan int)
    go busyWait(busyChan)
    select {
    case busyResult := <-busyChan:
        fmt.Println(busyResult)
    case <-time.After(time.Second):
        fmt.Println("timed out")
    }
}

func sleep(c chan<- int) {
    time.Sleep(10 * time.Second)
    c <- 0
}

func busyWait(c chan<- int) {
    for {
    }
    c <- 0
}

出力 (4 CPU プロセッサー):

1
4
timed out
timed out
于 2014-03-08T06:22:29.250 に答える