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