如何正确使用sync.Cond?

时间:2016-04-26 06:43:38

标签: go synchronization race-condition

我无法弄清楚如何正确使用sync.Cond。据我所知,在锁定Locker和调用条件的Wait方法之间存在竞争条件。此示例在主goroutine中的两条线之间添加了一个人为延迟,以模拟竞争条件:

package main

import (
    "sync"
    "time"
)

func main() {
    m := sync.Mutex{}
    c := sync.NewCond(&m)
    go func() {
        time.Sleep(1 * time.Second)
        c.Broadcast()
    }()
    m.Lock()
    time.Sleep(2 * time.Second)
    c.Wait()
}

[Run on the Go Playground]

这会立即引起恐慌:

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [semacquire]:
sync.runtime_Syncsemacquire(0x10330208, 0x1)
    /usr/local/go/src/runtime/sema.go:241 +0x2e0
sync.(*Cond).Wait(0x10330200, 0x0)
    /usr/local/go/src/sync/cond.go:63 +0xe0
main.main()
    /tmp/sandbox301865429/main.go:17 +0x1a0

我做错了什么?我该如何避免这种明显的竞争状况?我应该使用更好的同步构造吗?

编辑:我意识到我应该更好地解释我试图在这里解决的问题。我有一个长期运行的goroutine,下载一个大文件和许多其他goroutine,当它们可用时需要访问HTTP标头。这个问题比听起来更难。

我无法使用频道,因为只有一个goroutine会收到该值。其他一些goroutine会在它们已经可用之后很久就试图检索它们。

下载程序goroutine可以简单地将HTTP标头存储在变量中,并使用互斥锁来保护对它们的访问。但是,这并没有为其他goroutines提供一种等待"等等的方法。让它们变得可用。

我曾认为sync.Mutexsync.Cond一起可以实现这一目标,但似乎这是不可能的。

8 个答案:

答案 0 :(得分:13)

OP回答了他自己,但没有直接回答原来的问题,我将发布如何正确使用sync.Cond

如果每次写入和读取都有一个goroutine,那么你真的不需要sync.Cond - 单个sync.Mutex就足以在它们之间进行通信。 sync.Cond在多个读者等待共享资源可用的情况下非常有用。

var sharedRsc = make(map[string]interface{})
func main() {
    var wg sync.WaitGroup
    wg.Add(2)
    m := sync.Mutex{}
    c := sync.NewCond(&m)
    go func() {
        // this go routine wait for changes to the sharedRsc
        c.L.Lock()
        for len(sharedRsc) == 0 {
            c.Wait()
        }
        fmt.Println(sharedRsc["rsc1"])
        c.L.Unlock()
        wg.Done()
    }()

    go func() {
        // this go routine wait for changes to the sharedRsc
        c.L.Lock()
        for len(sharedRsc) == 0 {
            c.Wait()
        }
        fmt.Println(sharedRsc["rsc2"])
        c.L.Unlock()
        wg.Done()
    }()

    // this one writes changes to sharedRsc
    c.L.Lock()
    sharedRsc["rsc1"] = "foo"
    sharedRsc["rsc2"] = "bar"
    c.Broadcast()
    c.L.Unlock()
    wg.Wait()
}

Playground

话虽如此,如果情况允许,仍然建议使用频道传递数据。

注意:此处sync.WaitGroup仅用于等待goroutines完成执行。

答案 1 :(得分:8)

您需要确保在调用c.Wait后调用 后调用c .Broadcast。您的程序的正确版本将是:

package main

import (
    "fmt"
    "sync"
)

func main() {
    m := &sync.Mutex{}
    c := sync.NewCond(m)
    m.Lock()
    go func() {
        m.Lock() // Wait for c.Wait()
        c.Broadcast()
        m.Unlock()
    }()
    c.Wait() // Unlocks m
}

https://play.golang.org/p/O1r8v8yW6h

答案 2 :(得分:2)

package main

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

func main() {
    m := sync.Mutex{}
    m.Lock() // main gouroutine is owner of lock
    c := sync.NewCond(&m)
    go func() {
        m.Lock() // obtain a lock
        defer m.Unlock()
        fmt.Println("3. goroutine is owner of lock")
        time.Sleep(2 * time.Second) // long computing - because you are the owner, you can change state variable(s)
        c.Broadcast()               // State has been changed, publish it to waiting goroutines
        fmt.Println("4. goroutine will release lock soon (deffered Unlock")
    }()
    fmt.Println("1. main goroutine is owner of lock")
    time.Sleep(1 * time.Second) // initialization
    fmt.Println("2. main goroutine is still lockek")
    c.Wait() // Wait temporarily release a mutex during wating and give opportunity to other goroutines to change the state.
    // Because you don't know, whether this is state, that you are waiting for, is usually called in loop.
    m.Unlock()
    fmt.Println("Done")
}

http://play.golang.org/p/fBBwoL7_pm

答案 3 :(得分:2)

这是一个带有两个go例程的实际示例。他们一个接一个地开始,但是第二个在第一个广播的条件下等待,然后继续:

package main

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

func main() {
    lock := sync.Mutex{}
    lock.Lock()

    cond := sync.NewCond(&lock)

    waitGroup := sync.WaitGroup{}
    waitGroup.Add(2)

    go func() {
        defer waitGroup.Done()

        fmt.Println("First go routine has started and waits for 1 second before broadcasting condition")

        time.Sleep(1 * time.Second)

        fmt.Println("First go routine broadcasts condition")

        cond.Broadcast()
    }()

    go func() {
        defer waitGroup.Done()

        fmt.Println("Second go routine has started and is waiting on condition")

        cond.Wait()

        fmt.Println("Second go routine unlocked by condition broadcast")
    }()

    fmt.Println("Main go routine starts waiting")

    waitGroup.Wait()

    fmt.Println("Main go routine ends")
}

第二次执行例程可能在第一个执行例程开始之前执行,反之亦然,输出可能会略有不同:

Main go routine starts waiting
Second go routine has started and is waiting on condition
First go routine has started and waits for 1 second before broadcasting condition
First go routine broadcasts condition
Second go routine unlocked by condition broadcast
Main go routine ends

https://gist.github.com/fracasula/21565ea1cf0c15726ca38736031edc70

答案 4 :(得分:1)

看起来你c.Wait for Broadcast,你的时间间隔永远不会发生。 与

time.Sleep(3 * time.Second) //Broadcast after any Wait for it
c.Broadcast()

您的代码段似乎有用http://play.golang.org/p/OE8aP4i6gY。或者我错过了您尝试实现的内容?

答案 5 :(得分:1)

我终于发现了一种方法,它根本不涉及sync.Cond - 只是互斥体。

type Task struct {
    m       sync.Mutex
    headers http.Header
}

func NewTask() *Task {
    t := &Task{}
    t.m.Lock()
    go func() {
        defer t.m.Unlock()
        // ...do stuff...
    }()
    return t
}

func (t *Task) WaitFor() http.Header {
    t.m.Lock()
    defer t.m.Unlock()
    return t.headers
}

这是如何运作的?

互斥锁在任务开始时被锁定,确保调用WaitFor()的任何内容都会被阻止。一旦标题可用并且互斥锁解锁互斥锁,每次调用WaitFor()将一次执行一次。所有未来的调用(即使在goroutine结束后)都可以锁定互斥锁,因为它总是会被解锁。

答案 6 :(得分:0)

是的,您可以使用一个通道将Header传递给多个Go例程。

headerChan := make(chan http.Header)

go func() { // This routine can be started many times
    header := <-headerChan  // Wait for header
    // Do things with the header
}()

// Feed the header to all waiting go routines
for more := true; more; {
    select {
    case headerChan <- r.Header:
    default: more = false
    }
}

答案 7 :(得分:0)

在出色的书“ Concurrency in Go”中,他们提供了以下简单的解决方案,同时利用了关闭的通道将释放所有等待的客户端的事实。

package main
import (
    "fmt"
    "time"
)
func main() {
    httpHeaders := []string{}
    headerChan := make(chan interface{})
    var consumerFunc= func(id int, stream <-chan interface{}, funcHeaders *[]string)         
    {
        <-stream
        fmt.Println("Consumer ",id," got headers:", funcHeaders )   
    }
    for i:=0;i<3;i++ {
        go consumerFunc(i, headerChan, &httpHeaders)
    }
    fmt.Println("Getting headers...")
    time.Sleep(2*time.Second)
    httpHeaders=append(httpHeaders, "test1");
    fmt.Println("Publishing headers...")
    close(headerChan )
    time.Sleep(5*time.Second)
}

https://play.golang.org/p/cE3SiKWNRIt