美文网首页Go 极简教程
Go 使用互斥锁(sync.Mutex)实现线程安全的内存本地缓

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

作者: 光剑书架上的书 | 来源:发表于2022-05-05 13:22 被阅读0次

本地缓存实现


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)

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

相关文章

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

    本地缓存实现 sync.Mutex 说明 核心实现: CompareAndSwap 内嵌 struct 通常我们使...

  • Go 并发之互斥锁

    在 Go 语言中使用sync.Mutex 来实现互斥锁,示例代码如下: 输出结果为:

  • Go超时锁的设计和实现

    Go提供两种锁:sync.Mutex和sync.RWMutex。 sync.Mutex: 互斥锁。任意时刻,只能有...

  • 第13章 线程安全与锁优化

    第13章线程安全与锁优化 13.2线程安全 13.2.2线程安全的实现方法 1.互斥同步 互斥同步(Mutual ...

  • go-day5

    sync包 sync.Mutex 互斥锁 适用于并发访问公共资源的场景。 声明互斥锁 使用互斥锁 sync.RWM...

  • 多线程 -线程安全

    线程安全隐患 资源共享 解决数据错乱问题使用互斥锁互斥锁使用格式: @synchronized(锁对象) { //...

  • redis学习笔记(九) 分布式锁

    1. 引子 在多线程环境下,如果想对共享内存中的数据进行互斥访问且想保证线程安全,Java可以使用很多种锁来实现;...

  • C链表

    互斥锁:链表用在多线程中保证顺序,多个线程会操作同一个链表,互斥锁保证多线程操作的安全,互斥锁分情况使用,链表并不...

  • 线程同步与互斥

    Linux--线程编程 多线程编程-互斥锁 线程同步与互斥 互斥锁 信号量 条件变量 互斥锁 互斥锁的基本使用...

  • 1-6linux系统编程——线程间通信——练习:模拟银行的排队系

    线程间通信 信息交换: 使用多个线程都可见的内存区域 线程互斥锁: 保障有同一把锁保护的共享资源被多个线程互斥访问...

网友评论

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

    本文链接:https://www.haomeiwen.com/subject/jewnyrtx.html