zoukankan      html  css  js  c++  java
  • golang sync.Cond

    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()
    }
  • 相关阅读:
    记忆化搜索 E
    网络流 O
    线段树 B数据结构 牛客练习赛28
    N
    线段树 G
    K
    F
    补一下昨天的博客 J
    selenium-1-python
    selenium入门知识
  • 原文地址:https://www.cnblogs.com/rojas/p/4402638.html
Copyright © 2011-2022 走看看