Go语言-并发通信
package main import "fmt" import "sync" import "runtime" var counter int = 0 func Count(lock *sync.Mutex) { lock.Lock() counter++ fmt.Println(counter) lock.Unlock() } func main() { lock := &sync.Mutex{} for i := 0; i < 10; i++ { go Count(lock) } for { lock.Lock() c := counter lock.Unlock() runtime.Gosched() if c >= 10 { break } } }
在线演示
https://c.runoob.com/compile/21
原文链接:/go/188804.html