golang sync.Mutex(2)

package main

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

type User struct {
    Name   string
    Locker *sync.Mutex
}

func (u *User) SetName(wati *sync.WaitGroup, name string) {
    defer func() {
        fmt.Println("Unlock set name:", name)
        u.Locker.Unlock()
        wati.Done()
    }()

    u.Locker.Lock()
    fmt.Println("Lock set name:", name)
    time.Sleep(1 * time.Second)
    u.Name = name
}

func (u *User) GetName(wati *sync.WaitGroup) {
    defer func() {
        fmt.Println("Unlock get name:", u.Name)
        u.Locker.Unlock()
        wati.Done()
    }()

    u.Locker.Lock()
    fmt.Println("Lock get name:", u.Name)
    time.Sleep(1 * time.Second)
}

func main() {
    user := User{}
    user.Locker = new(sync.Mutex)
    wait := &sync.WaitGroup{}
    names := []string{"a", "b", "c"}
    for _, name := range names {
        wait.Add(2)
        go user.SetName(wait, name)
        go user.GetName(wait)
    }

    wait.Wait()
}

转自:http://www.liguosong.com/2014/05/07/golang-sync-mutex/

时间: 2024-10-09 13:49:40

golang sync.Mutex(2)的相关文章

golang sync.Mutex

//go func 和主线程之间的关系是并行和竞争关系 package main import ( "fmt" "sync" "time" ) var l sync.Mutex var m *sync.Mutex func main() { m = new(sync.Mutex) go lock(1) time.Sleep(3 * time.Second) fmt.Printf("%s\n", "exit!"

不得不知道的golang之sync.Mutex互斥锁源码分析

针对Golang 1.9的sync.Mutex进行分析,与Golang 1.10基本一样除了将panic改为了throw之外其他的都一样.源代码位置:sync\mutex.go.可以看到注释如下: Mutex can be in 2 modes of operations: normal and starvation. In normal mode waiters are queued in FIFO order, but a woken up waiter does not own the m

golang 中 sync.Mutex 和 sync.RWMutex

介绍 golang 中的 sync 包实现了两种锁: Mutex:互斥锁 RWMutex:读写锁,RWMutex 基于 Mutex 实现 Mutex(互斥锁) Mutex 为互斥锁,Lock() 加锁,Unlock() 解锁 在一个 goroutine 获得 Mutex 后,其他 goroutine 只能等到这个 goroutine 释放该 Mutex 使用 Lock() 加锁后,不能再继续对其加锁,直到利用 Unlock() 解锁后才能再加锁 在 Lock() 之前使用 Unlock() 会导

sync.Mutex (对map加锁)

A Tour of Go concurrency >> sync.Mutex https://tour.golang.org/concurrency/9 golang 里的 map 并不是 concurrency-safe 的,也就是说,当多个 goroutine 对同一个 map 进行操作时,有可能发生冲突. 教程中给出了一个例子,对 map 加锁避免冲突: 其中,如果把 go c.Inc("somekey") 改成 go func() { c.v["somek

Golang sync.NewCond条件锁的用法

package main import ( "fmt" "sync" "time" ) func main() { c := sync.NewCond(&sync.Mutex{}) queue := make([]interface{}, 0, 10) removeFromQueue := func(delay time.Duration) { time.Sleep(delay) c.L.Lock() queue = queue[1:]

golang sync.RWMutex

sync.RWMutex package main import ( "fmt" "runtime" "sync" ) func clickWithMutex(total *int, m *sync.RWMutex, ch chan int) { for i := 0; i < 1000; i++ { m.Lock() *total += 1 m.Unlock() //这里是写 下面是读,外层还有线程的竞争 if i == 500 { m.

golang sync.WaitGroup

//阻塞,直到WaitGroup中的所以过程完成. import ( "fmt" "sync" ) func wgProcess(wg *sync.WaitGroup, id int) { fmt.Printf("process:%d is going!\n", id) //if id == 2 { // return //} wg.Done() } func main() { //var wg sync.WaitGroup wg := new(

golang sync.Pool包的使用和一些注意地方

package main; import ( "sync" "fmt" "net" "runtime" ) //sync.Pool是一个可以存或取的临时对象集合 //sync.Pool可以安全被多个线程同时使用,保证线程安全 //注意.注意.注意,sync.Pool中保存的任何项都可能随时不做通知的释放掉,所以不适合用于像socket长连接或数据库连接池. //sync.Pool主要用途是增加临时对象的重用率,减少GC负担.

golang sync.Once

package main import ( "fmt" "sync" "time" ) var counter int = 0 func main() { var once sync.Once onceBody := func() { time.Sleep(3e9) fmt.Println("Only once") } done := make(chan bool) for i := 0; i < 10; i++ { j