自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用 sync.Cond 來協調并發(fā) goroutine 的訪問共享資源

開發(fā) 前端
互斥鎖(sync.Mutex)用于保護臨界區(qū)和共享資源,而 sync.Cond? 則用于協調多個 goroutine? 的執(zhí)行順序?;コ怄i只能一個 goroutine? 持有鎖,其他 goroutine? 必須等待鎖被釋放才能繼續(xù)執(zhí)行。而 sync.Cond? 可以讓等待的 goroutine 在條件滿足時被喚醒,進而繼續(xù)執(zhí)行。

使用 sync.Cond 解決并發(fā)訪問共享資源問題

在并發(fā)編程中,當多個 goroutine 需要訪問共享資源時,我們需要使用一些機制來協調它們的執(zhí)行順序,以避免競態(tài)條件和數據不一致的問題。在 Go 語言中,sync.Cond 條件變量就是一種常用的機制,它可以用來等待和通知其他 goroutine。

sync.Cond 和互斥鎖的區(qū)別

互斥鎖(sync.Mutex)用于保護臨界區(qū)和共享資源,而 sync.Cond 則用于協調多個 goroutine 的執(zhí)行順序?;コ怄i只能一個 goroutine 持有鎖,其他 goroutine 必須等待鎖被釋放才能繼續(xù)執(zhí)行。而 sync.Cond 可以讓等待的 goroutine 在條件滿足時被喚醒,進而繼續(xù)執(zhí)行。

sync.Cond 的四個方法

sync.Cond 的定義如下:

// 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.
//
// A Cond must not be copied after first use.
type Cond struct {
        noCopy noCopy

        // L is held while observing or changing the condition
        L Locker

        notify  notifyList
        checker copyChecker
}

每個 Cond 實例都會關聯一個鎖 L(互斥鎖 *Mutex,或讀寫鎖 *RWMutex),當修改條件或者調用 Wait 方法時,必須加鎖。

1. NewCond 創(chuàng)建實例

func NewCond(l Locker) *Cond

NewCond 方法用于創(chuàng)建一個 Cond 實例,并關聯一個鎖(互斥鎖或讀寫鎖)。

2. Broadcast 廣播喚醒所有等待的 goroutine

// Broadcast wakes all goroutines waiting on c.
//
// It is allowed but not required for the caller to hold c.L
// during the call.
func (c *Cond) Broadcast()

Broadcast 方法用于喚醒所有等待條件變量 c 的 goroutine。它不需要持有鎖來調用。

3. Signal 喚醒一個等待的 goroutine

// Signal wakes one goroutine waiting on c, if there is any.
//
// It is allowed but not required for the caller to hold c.L
// during the call.
func (c *Cond) Signal()

Signal 方法用于喚醒一個等待條件變量 c 的 goroutine。它不需要持有鎖來調用。

4. Wait 等待條件變量滿足

// 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.
//
// 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:
//
//    c.L.Lock()
//    for !condition() {
//        c.Wait()
//    }
//    ... make use of condition ...
//    c.L.Unlock()
//
func (c *Cond) Wait()

Wait 方法會自動釋放鎖,并掛起當前的 goroutine,直到條件變量 c 被 Broadcast 或 Signal 喚醒。被喚醒后,Wait 方法會重新獲得鎖,并繼續(xù)執(zhí)行后續(xù)的代碼。

使用示例

下面是一個使用 sync.Cond 的示例,實現了一個簡單的讀寫同步機制:

package main

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

var done = false

func read(str string, c *sync.Cond) {
    c.L.Lock()
    for !done {
        c.Wait()
    }
    fmt.Println(str, "start reading")
    c.L.Unlock()
}

func write(str string, c *sync.Cond) {
    fmt.Println(str, "start writing")
    time.Sleep(2 * time.Second)
    c.L.Lock()
    done = true
    c.L.Unlock()
    fmt.Println(str, "wake up all")
    c.Broadcast()
}

func main() {
    m := &sync.Mutex{}
    c := sync.NewCond(m)

    go read("reader1", c)
    go read("reader2", c)
    write("writer", c)

    time.Sleep(5 * time.Second)
}

在這個示例中,有兩個讀取協程(reader1 和 reader2)和一個寫入協程(writer)。寫入協程在執(zhí)行后會通知所有等待的讀取協程,讀取協程在條件滿足時才能開始讀取。

輸出結果如下:

writer start writing
writer wake up all
reader2 start reading
reader1 start reading

通過使用 sync.Cond,我們可以很方便地實現多個 goroutine 之間的等待和通知機制,從而更好地協調并發(fā)訪問共享資源的執(zhí)行順序。

責任編輯:武曉燕 來源: 愛發(fā)白日夢的后端
相關推薦

2023-06-26 08:28:35

Sync.CondGolang

2021-07-06 07:46:07

Go語言編程

2021-05-21 08:21:57

Go語言基礎技術

2009-01-08 09:54:00

2023-12-24 12:33:20

互斥鎖Go代碼

2021-09-30 09:21:28

Go語言并發(fā)編程

2011-03-02 09:59:01

Ubuntuvsftpd

2020-02-21 20:21:45

線程共享資源

2011-09-01 09:18:36

2020-09-16 07:56:28

多線程讀寫鎖悲觀鎖

2021-04-02 09:50:14

微服務分布式鎖Java

2023-06-16 08:36:25

多線程編程數據競爭

2020-03-06 08:00:06

Zookeeper分布式系統

2023-05-18 08:38:13

Java鎖機制

2023-06-02 08:29:24

https://wwMutex

2023-11-10 08:44:13

分布式鎖分布式系統

2023-12-07 12:32:57

Java死鎖線程

2023-07-27 08:59:19

線程同步Python

2009-04-26 22:09:51

windowsserver共享

2013-11-13 10:16:35

局域網共享文件
點贊
收藏

51CTO技術棧公眾號