packagemain import( "fmt" "sync" "runtime" ) varcounterint=0 funcCount(lock*sync.Mutex){ lock.Lock() counter++ fmt.Println(counter) lock.Unlock() } funcmain(){ lock:=&sync.Mutex{} fori:=0;i<10;i++{ goCount(lock)//并发执行 } for{ lock.Lock()//锁定 c:=counter lock.Unlock()//解锁 runtime.Gosched()//把时间片让给别人 ifc>=10{ break } } }原文链接:https://www.f2er.com/go/190743.html