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