package main

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

func main() {
    wait := sync.WaitGroup{}
    locker := new(sync.Mutex)
    cond := sync.NewCond(locker)

    for i := 0; i < 3; i++ {
        go func(i int) {
            defer wait.Done()
            wait.Add(1)
            cond.L.Lock()
            fmt.Println("Waiting start...")
            cond.Wait()
            fmt.Println("Waiting end...")
            cond.L.Unlock()

            fmt.Println("Goroutine run. Number:", i)
        }(i)
    }

    time.Sleep(2e9)
    cond.L.Lock()
    cond.Signal()
    cond.L.Unlock()

    time.Sleep(2e9)
    cond.L.Lock()
    cond.Signal()
    cond.L.Unlock()

    time.Sleep(2e9)
    cond.L.Lock()
    cond.Signal()
    cond.L.Unlock()

    wait.Wait()
}

 

相关文章:

  • 2021-08-11
  • 2021-10-13
  • 2023-03-20
  • 2023-01-09
  • 2023-01-31
  • 2022-12-23
  • 2023-03-21
猜你喜欢
  • 2022-01-17
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2023-01-10
  • 2021-09-19
相关资源
相似解决方案