golang 條件變量&發送通知&接受通知&廣播

3個指針方法:git

Wait()  等待goroutine 等待接受通知,Single() Broadcast() 解除阻塞
    Single() 發送通知,若是存在多個goroutine,只會任意選擇一個
    Broadcast() 廣播 方法給全部人

下面時wait方法底層執行流程github

  1. 嘗試解鎖
  2. wait 嘗試解鎖,解完鎖以後等待其餘給發通知,===> 進入了阻塞狀態,等待被喚醒: single(),broadcast()
  3. 一旦被喚醒後,又會鎖定
func (c *Cond) Wait() {
    c.checker.check()
    t := runtime_notifyListAdd(&c.notify)
    c.L.Unlock()
    runtime_notifyListWait(&c.notify, t)
    c.L.Lock()
}

操做案例:golang

sync.Cond
相關文章
相關標籤/搜索