package main
import (
"fmt"
"runtime"
"sync"
)
var count int = 0
func counter(lock *sync.Mutex) {
lock.Lock()
count++
fmt.Println(count)
lock.Unlock()
}
func main() {
lock := &sync.Mutex{} //传递指针是为了防止 函数内的锁和 调用锁不一致
for i := 0; i < 100000; i++ {
go counter(lock)
}
for {
lock.Lock()
c := count
lock.Unlock()
runtime.Gosched() //把时间片给别的goroutine 未来某个时刻运行该routine
if c >= 100000 {
fmt.Println("gorountine end")
break
}
}
}