Go语言的排它锁sync.Mutex

package main

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

var m *sync.Mutex

func main() {
    m = new(sync.Mutex)

    go lock(1)
    time.Sleep(time.Second)

    lock(2)

    fmt.Printf("%s\n", "exit!")
}

func lock(i int){
    println(i, "lock start")

    m.Lock()
    println(i, "lock")

    time.Sleep(10 * time.Second)

    m.Unlock()
    println(i, "unlock")
}
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页