funcmain() { var wg sync.WaitGroup wg.Add(2) m := sync.Mutex{} c := sync.NewCond(&m) //运动员1 gofunc() { // this go routine wait for changes to the sharedRsc c.L.Lock() fmt.Println("goroutine1 pass Lock") for sharedRsc == false { fmt.Println("goroutine1 wait") c.Wait() } fmt.Println("goroutine1", sharedRsc) c.L.Unlock() wg.Done() }()
//运动员2 gofunc() { // this go routine wait for changes to the sharedRsc c.L.Lock() fmt.Println("goroutine2 pass Lock") for sharedRsc == false { fmt.Println("goroutine2 wait") c.Wait() } fmt.Println("goroutine2", sharedRsc) c.L.Unlock() wg.Done() }()