概述


goroutine 可能切换的点
-
非强占式
-
I/O ,select
-
channel
-
等待锁
-
调用函数
-
runtime.Gosched()
-
只是参考,不能保证切换

代码channel
package main
import (
"fmt"
"time"
)
func worker(id int, c chan int) {
for n := range c {
fmt.Printf("Worker %d received %c\n",
id, n)
}
}
func createWorker(id int) chan<- int {
c := make(chan int)
go worker(id, c)
return c
}

本文介绍了Go语言中的协程(goroutine)及其可能的切换点,包括非强占式调度、I/O操作、select语句、channel的使用以及等待锁和调用函数。还特别讨论了如何通过runtime.Gosched()来建议调度器进行协程切换。文中通过具体的channel和select代码示例,以及done信号的处理,提供了深入理解Go协程和并发控制的实践指导。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



