sync.Mutex 与 sync.WaitGroup 使用示例

资讯 2年前 (2022) 千度导航
20 0 0

使用 sync.Mutex 与 sync.WaitGroup

线程不安全的用法:

{
    var wg sync.WaitGroup
    count := 0
    for i := 0; i < 10; i++ {
        // wg add 1 goroutine
        wg.Add(1)
        // goroutine
        go func() {
            defer wg.Done()
            for i := 0; i < 10000; i++ {
                // unsafe increment
                count++
            }
        }()
    }

    wg.Wait()
    fmt.Println(count)
}

加锁,线程安全:

{
    var myLock sync.Mutex
    var wg sync.WaitGroup
    count := 0
    for i := 0; i < 10; i++ {
        // wg add 1 goroutine
        wg.Add(1)
        // goroutine
        go func() {
            defer wg.Done()
            for i := 0; i < 10000; i++ {
                // safe increment
                myLock.Lock()
                count++
                myLock.Unlock()
            }
        }()
    }

    wg.Wait()
    fmt.Println(count)
}

其中一个运行输出:
53546
100000

自定义一个safe Counter

package counter

import (
    "log"
    "sync"
)

type Counter struct {
    mu    *sync.Mutex
    Value int
}

func NewCounter(value int) *Counter {
    return &Counter{
        new(sync.Mutex), 0,
    }
}

func (c *Counter) Increment() {
    c.mu.Lock()
    // defer func
    defer func() {
        c.mu.Unlock()
        log.Printf("mu sync.Mutex Unlocked!")
    }()
    // safe increment Value
    c.Value++
}

使用 safe Counter

{
    var wg sync.WaitGroup
    count := counter.NewCounter(0)
    for i := 0; i < 10; i++ {
        // wg add 1 goroutine
        wg.Add(1)
        // goroutine
        go func() {
            defer wg.Done()
            for i := 0; i < 10000; i++ {
                // safe increment
                count.Increment()
            }
        }()
    }

    wg.Wait()
    fmt.Println(count.Value)
}

正确输出:
100000

Go 使用互斥锁(sync.Mutex)实现线程安全的内存本地缓存(LocalCache)

本地缓存实现


var cache = struct { // 声明 struct 字面量 cahce (匿名结构体)
    sync.Mutex                   // 互斥锁, 内嵌 Struct
    caches     map[string]string // kv 内存存储
}{
    caches: make(map[string]string), // 初始化 kv 存储
}

// GetCache safe get memory cache
func GetCache(key string) string {
    cache.Lock()           // 锁住
    v := cache.caches[key] // 获取缓存值
    defer cache.Unlock()   // 释放锁
    return v
}

// PutCache safe put memory cache
func PutCache(key string, val string) {
    cache.Lock()            // 锁住
    cache.caches[key] = val // 设置缓存值
    defer cache.Unlock()    // 释放锁
}

func main() {
    for i := 0; i < 10; i++ {
        PutCache(fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i))
    }
    var v = GetCache("key1")
    fmt.Println(v) // value1
}

sync.Mutex 说明

核心实现: CompareAndSwap

// Package sync provides basic synchronization primitives such as mutual
// exclusion locks. Other than the Once and WaitGroup types, most are intended
// for use by low-level library routines. Higher-level synchronization is
// better done via channels and communication.
//
// Values containing the types defined in this package should not be copied.
package sync

import (
    "internal/race"
    "sync/atomic"
    "unsafe"
)

func throw(string) // provided by runtime

// A Mutex is a mutual exclusion lock.
// The zero value for a Mutex is an unlocked mutex.
//
// A Mutex must not be copied after first use.
type Mutex struct {
    state int32
    sema  uint32
}

// A Locker represents an object that can be locked and unlocked.
type Locker interface {
    Lock()
    Unlock()
}

const (
    mutexLocked = 1 << iota // mutex is locked
    mutexWoken
    mutexStarving
    mutexWaiterShift = iota

    // Mutex fairness.
    //
    // 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 mutex and competes with new arriving goroutines over
    // the ownership. New arriving goroutines have an advantage -- they are
    // already running on CPU and there can be lots of them, so a woken up
    // waiter has good chances of losing. In such case it is queued at front
    // of the wait queue. If a waiter fails to acquire the mutex for more than 1ms,
    // it switches mutex to the starvation mode.
    //
    // In starvation mode ownership of the mutex is directly handed off from
    // the unlocking goroutine to the waiter at the front of the queue.
    // New arriving goroutines don't try to acquire the mutex even if it appears
    // to be unlocked, and don't try to spin. Instead they queue themselves at
    // the tail of the wait queue.
    //
    // If a waiter receives ownership of the mutex and sees that either
    // (1) it is the last waiter in the queue, or (2) it waited for less than 1 ms,
    // it switches mutex back to normal operation mode.
    //
    // Normal mode has considerably better performance as a goroutine can acquire
    // a mutex several times in a row even if there are blocked waiters.
    // Starvation mode is important to prevent pathological cases of tail latency.
    starvationThresholdNs = 1e6
)

// Lock locks m.
// If the lock is already in use, the calling goroutine
// blocks until the mutex is available.
func (m *Mutex) Lock() {
    // Fast path: grab unlocked mutex.
    if atomic.CompareAndSwapInt32(&m.state, 0, mutexLocked) {
        if race.Enabled {
            race.Acquire(unsafe.Pointer(m))
        }
        return
    }
    // Slow path (outlined so that the fast path can be inlined)
    m.lockSlow()
}



// Unlock unlocks m.
// It is a run-time error if m is not locked on entry to Unlock.
//
// A locked Mutex is not associated with a particular goroutine.
// It is allowed for one goroutine to lock a Mutex and then
// arrange for another goroutine to unlock it.
func (m *Mutex) Unlock() {
    if race.Enabled {
        _ = m.state
        race.Release(unsafe.Pointer(m))
    }

    // Fast path: drop lock bit.
    new := atomic.AddInt32(&m.state, -mutexLocked)
    if new != 0 {
        // Outlined slow path to allow inlining the fast path.
        // To hide unlockSlow during tracing we skip one extra frame when tracing GoUnblock.
        m.unlockSlow(new)
    }
}

...

内嵌 struct

通常我们使用"有名"的 struct 嵌套如下:

package main
 
import "fmt"
 
type People struct{
    name string
    age int8
}
 
type Employee struct{
    profile *People
    salary float32
}
func main() {
   p := &People{
           name: "Mr.Lee",
           age: 32,
       }
   e := &Employee{
       profile: p,
       salary: 600000,
   }
    fmt.Println("Hello world!", e.profile.name)
}

go语言中虽然没有继承,但是可以结构内嵌,达到类似继承的效果:

type Info struct {
    sex int
    name string
    age int
    address string
}
 
type User struct{
    like string
    Info
}
 
type Admin struct {
    unlike string
    Info
}
 
user:= User{}
user.sex=0
user.address="广州市"
user.like="游戏"
f.Println(user)
    
    
admin:= Admin{Info:Info{sex:1}}//还可以这样声明一些属性值,因为Info是结构体,匿名,所以需要这样声明
admin.address="广州市"
admin.unlike="游戏"
f.Println(admin)

如果嵌入结构的字段和外部结构的字段相同,那么, 为了避免命名冲突, 想要修改嵌入结构的字段值需要加上外部结构中声明的嵌入结构名称.

版权声明:千度导航 发表于 2022年7月2日 23:11。
转载请注明:sync.Mutex 与 sync.WaitGroup 使用示例 | 千度百科

相关文章