在 2 个 goroutine 之间同步

Sync between 2 goroutines

我的任务是同步 2 个 goroutine,因此输出应该如下所示:

foobarfoobarfoobarfoobar

。问题是,当我调用它们时,它们完全随机出现。这是我的代码:

package main

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

type ConcurrentPrinter struct {
    sync.WaitGroup
    sync.Mutex
}


func (cp *ConcurrentPrinter) printFoo(times int) {
    cp.WaitGroup.Add(times)
    go func() {
        cp.Lock()
        fmt.Print("foo")
        cp.Unlock()
    }()
}
func (cp *ConcurrentPrinter) printBar(times int) {
    cp.WaitGroup.Add(times)
    go func() {
        cp.Lock()
        fmt.Print("bar")
        cp.Unlock()
    }()
}

func main() {
    times := 10
    cp := &ConcurrentPrinter{}

    for i := 0; i <= times; i++ {
        cp.printFoo(i)
        cp.printBar(i)
    }
    time.Sleep(10 * time.Millisecond)
}

如评论中所述,使用 goroutines 可能不是您要实现的目标的最佳用例 - 因此这可能是 XY problem

话虽如此,如果你想确保两个独立的 goroutines 以交替顺序交织它们的工作,你可以实现一组“ping-pong”互斥锁:

var ping, pong sync.Mutex

pong.Lock() // ensure the 2nd goroutine waits & the 1st goes first

go func() {
    for {
        ping.Lock()
        foo()
        pong.Unlock()
    }
}()

go func() {
    for {
        pong.Lock()
        bar()
        ping.Unlock()
    }
}()

https://go.dev/play/p/VO2LoMJ8fek

使用频道:

func printFoo(i int, ch chan<- bool, wg *sync.WaitGroup) {
    wg.Add(1)
    go func() {
        defer wg.Done()
        fmt.Print("foo")
        ch <- true
    }()
}
func printBar(i int, ch chan<- bool, wg *sync.WaitGroup) {
    wg.Add(1)
    go func() {
        defer wg.Done()
        fmt.Print("bar")
        ch <- true
    }()

}

func main() {
    times := 4
    firstchan := make(chan bool)
    secondchan := make(chan bool)
    var wg sync.WaitGroup
    for i := 0; i <= times; i++ {
        printFoo(i, firstchan, &wg)
        <-firstchan
        printBar(i, secondchan, &wg)
        <-secondchan
    }
    wg.Wait()
}

https://go.dev/play/p/MlZ9dHkUXGb