goland cond數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)

1、cond結(jié)構(gòu)介紹

// Cond implements a condition variable, a rendezvous point
// for goroutines waiting for or announcing the occurrence
// of an event.
// Cond結(jié)構(gòu)實(shí)現(xiàn)一個(gè)條件變量和約定指針,為了等待協(xié)程或者通知一個(gè)事件的發(fā)生。
// Each Cond has an associated Locker L (often a *Mutex or *RWMutex),
// which must be held when changing the condition and
// when calling the Wait method.
// 每一個(gè)cond都有一個(gè)與之關(guān)聯(lián)Locker L的鎖(通常是讀鎖或者讀寫鎖)
// 當(dāng)條件改變條件和調(diào)用Wait方法時(shí),這個(gè)鎖是鎖住的。
// A Cond must not be copied after first use.
// 這個(gè)Cond變量,在初始化之后,不能再被復(fù)制。
type Cond struct {
    noCopy noCopy
    // L is held while observing or changing the condition
    // 當(dāng)狀態(tài)一直是改變或者是觀察的狀態(tài)時(shí),L將被鎖住
       L Locker

    notify  notifyList  // 事件通知列表
    checker copyChecker   // 檢查變量是否被copy
}

2、cond方法介紹

// NewCond returns a new Cond with Locker l.
// 實(shí)例化一個(gè)帶有Locker的Cond變量
func NewCond(l Locker) *Cond {
    return &Cond{L: l}
}

// Wait atomically unlocks c.L and suspends execution
// of the calling goroutine. After later resuming execution,
// Wait locks c.L before returning. Unlike in other systems,
// Wait cannot return unless awoken by Broadcast or Signal.
// 等待釋放鎖和暫緩調(diào)用這個(gè)協(xié)程,在恢復(fù)執(zhí)行之后,wait將一直鎖住,
//這點(diǎn)不像其他操作系統(tǒng),如果不調(diào)用Broadcast或者Signal, Wait將不返回
// Because c.L is not locked when Wait first resumes, the caller
// typically cannot assume that the condition is true when
// Wait returns. Instead, the caller should Wait in a loop:
// 因?yàn)閃ait第一次恢復(fù)時(shí),c.L是不被鎖住的;當(dāng)Wait返回時(shí),調(diào)用者通常不認(rèn)為這是真的
// 相反,調(diào)用者,應(yīng)該在循環(huán)中等待。
//    c.L.Lock()
//    for !condition() {
//        c.Wait()
//    }
//    ... make use of condition ...
//    c.L.Unlock()
//
func (c *Cond) Wait() {
    c.checker.check()
    t := runtime_notifyListAdd(&c.notify)
    c.L.Unlock()
    runtime_notifyListWait(&c.notify, t)
    c.L.Lock()
}

// Signal wakes one goroutine waiting on c, if there is any.
// Signal 喚醒一個(gè)作用在c上等待的協(xié)程,無論是哪一個(gè)。
// It is allowed but not required for the caller to hold c.L
// during the call.
// 在調(diào)用期間,c.L 是不一定要持有這個(gè)c.L
func (c *Cond) Signal() {
    c.checker.check()
    runtime_notifyListNotifyOne(&c.notify)
}

// Broadcast wakes all goroutines waiting on c.
// Broadcast 喚醒所有作用于c上而等待的協(xié)程。
// It is allowed but not required for the caller to hold c.L
// during the call.
func (c *Cond) Broadcast() {
    c.checker.check()
    runtime_notifyListNotifyAll(&c.notify)
}

3、代碼示例

package main

import (
    "sync"
    "fmt"
    "time"
)

var locker = new(sync.Mutex)
var cond = sync.NewCond(locker)
func main()  {
    for i:=0; i < 5; i++ {
        go func(x int) {
            cond.L.Lock() // wait 前,必須要先加鎖
            defer cond.L.Unlock()
            cond.Wait()
            fmt.Println(x)
            time.Sleep(time.Second * 1)
        }(i)
    }
    time.Sleep(time.Second*1)
    fmt.Println("Signal....")
    cond.Signal() // 下發(fā)一個(gè)通知給已經(jīng)獲取鎖的goroutine
    time.Sleep(time.Second * 3)
    cond.Signal() // 3 秒之后,下發(fā)一個(gè)通知給已經(jīng)獲取鎖的goroutine
    time.Sleep(time.Second *3)
    cond.Broadcast() // 3 秒
    fmt.Println("Broadcast...")
    time.Sleep(time.Second * 60)
}

5、應(yīng)用場景

  • 限流器

參考:類庫dropbox中,rate_limiter.go

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地?cái)?shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,172評(píng)論 3 119
  • 用到的組件 1、通過CocoaPods安裝 2、第三方類庫安裝 3、第三方服務(wù) 友盟社會(huì)化分享組件 友盟用戶反饋 ...
    SunnyLeong閱讀 15,147評(píng)論 1 180
  • 今天看完了【我不是藥神】,感觸很深,特別是自己做保險(xiǎn)的原因,全程有一點(diǎn)壓抑和心酸。 有兩個(gè)鏡頭觸動(dòng)到我,一個(gè)是眼鏡...
    徐倩_b0d3閱讀 346評(píng)論 1 0
  • 十三 離諾我們行到哪了 前面就到洪州境內(nèi)了 到了以后我們暫且休息一下 我去打聽一下有什么情況 你還記得至凈師傅跟我...
    YuXiangssg閱讀 381評(píng)論 0 0
  • 2018擇校方案,小編給大家分析下思路,第一看自己,第二看行業(yè),第三看院校和專業(yè)。記住順序,記反了你基本上就沒得選...
    smile_eye閱讀 289評(píng)論 0 1

友情鏈接更多精彩內(nèi)容