刚刚学习了golang GMP模型,现在我了解了goroutines、操作系统线程和golang上下文/处理器如何相互协作。但我还是不明白什么时候会产生M和P?
例如,我有一个测试代码来在数据库上运行一些操作,并且有两个测试用例(两批 goroutine):
func Test_GMP(t *testing.T) {
for _ = range []struct {
name string
}{
{"first batch"},
{"second batch"},
} {
goroutineSize := 50
done := make(chan error, goroutineSize)
for i := 0; i < goroutineSize; i++ {
go func() {
// do some databases operations...
// each goroutine should be blocked here for some time...
// propogate the result
done <- nil
}()
}
for i := 0; i < goroutineSize; i++ {
select {
case err …Run Code Online (Sandbox Code Playgroud) 根据GOPL,“选择等待直到某些情况的通信准备好继续”,那么未选择的通道会发生什么?此外,向“未选择”通道发送消息的 goroutine 是否会卡住从而导致 goroutine 泄漏?
还是因为“unselected”通道不可达,被GC回收(立即?),卡住的goroutine也被回收?