golang在并发下GOMAXPROCS(1)

查灵 ·
這有些問題,goroutine本來就都有被掛起機會,即使單cpu下當你for迴圈次數大時依然會被掛起 參考以下程式碼即使只叫起兩個goroutine依舊會干擾,若要保證順序性應該善用channel來達成 ```go package main import ( "fmt" "runtime" "sync" ) func main() { runtime.GOMAXPROCS(1) var wg sync.WaitGroup wg.Add(2) max := 100000 fmt.Println("go start") go func() { defer wg.Done() count := 0 for { if count > max { break } fmt.Println("No.1 G:", count) count++ } }() go func() { defer wg.Done() count := 0 for { if count > max { break } fmt.Println("No.2 G:", count) count++ } }() wg.Wait() fmt.Println("Done") } ```
#1