如何在Go中获取互斥体地图?

时间:2016-12-02 11:39:23

标签: go thread-safety

我正在围绕数据库创建一个缓存包装器。为了解释可能缓慢的数据库调用,我想到了每个密钥的互斥锁(伪Go代码):

mutexes = map[string]*sync.Mutex // instance variable

mutexes[key].Lock()
defer mutexes[key].Unlock()

if value, ok := cache.find(key); ok {
   return value
}
value = databaseCall(key)
cache.save(key, value)
return value

但是我不希望我的地图增长太多。 我的缓存是一个LRU ,我希望有一个固定的大小,原因是这里没有提到的其他一些原因。我想做点什么

delete(mutexes, key)

当钥匙上的所有锁都结束但......对我来说看起来线程安全 ......我该怎么做?

注意:我发现了这个问题 In Go, can we synchronize each key of a map using a lock per key?但没有回答

2 个答案:

答案 0 :(得分:2)

互斥锁映射是完成此操作的有效方法,但是映射本身也必须同步。引用计数可用于跟踪并发使用的条目,并在不再需要时将其删除。这是互斥体的工作图,其中包含测试和基准。

mapofmu.go

// Package mapofmu provides locking per-key.
// For example, you can acquire a lock for a specific user ID and all other requests for that user ID
// will block until that entry is unlocked (effectively your work load will be run serially per-user ID),
// and yet have work for separate user IDs happen concurrently.
package mapofmu

import (
    "fmt"
    "sync"
)

// M wraps a map of mutexes.  Each key locks separately.
type M struct {
    ml sync.Mutex              // lock for entry map
    ma map[interface{}]*mentry // entry map
}

type mentry struct {
    m   *M          // point back to M, so we can synchronize removing this mentry when cnt==0
    el  sync.Mutex  // entry-specific lock
    cnt int         // reference count
    key interface{} // key in ma
}

// Unlocker provides an Unlock method to release the lock.
type Unlocker interface {
    Unlock()
}

// New returns an initalized M.
func New() *M {
    return &M{ma: make(map[interface{}]*mentry)}
}

// Lock acquires a lock corresponding to this key.
// This method will never return nil and Unlock() must be called
// to release the lock when done.
func (m *M) Lock(key interface{}) Unlocker {

    // read or create entry for this key atomically
    m.ml.Lock()
    e, ok := m.ma[key]
    if !ok {
        e = &mentry{m: m, key: key}
        m.ma[key] = e
    }
    e.cnt++ // ref count
    m.ml.Unlock()

    // acquire lock, will block here until e.cnt==1
    e.el.Lock()

    return e
}

// Unlock releases the lock for this entry.
func (me *mentry) Unlock() {

    m := me.m

    // decrement and if needed remove entry atomically
    m.ml.Lock()
    e, ok := m.ma[me.key]
    if !ok { // entry must exist
        m.ml.Unlock()
        panic(fmt.Errorf("Unlock requested for key=%v but no entry found", me.key))
    }
    e.cnt--        // ref count
    if e.cnt < 1 { // if it hits zero then we own it and remove from map
        delete(m.ma, me.key)
    }
    m.ml.Unlock()

    // now that map stuff is handled, we unlock and let
    // anything else waiting on this key through
    e.el.Unlock()

}

mapofmu_test.go:

package mapofmu

import (
    "math/rand"
    "strconv"
    "strings"
    "sync"
    "testing"
    "time"
)

func TestM(t *testing.T) {

    r := rand.New(rand.NewSource(42))

    m := New()
    _ = m

    keyCount := 20
    iCount := 10000
    out := make(chan string, iCount*2)

    // run a bunch of concurrent requests for various keys,
    // the idea is to have a lot of lock contention
    var wg sync.WaitGroup
    wg.Add(iCount)
    for i := 0; i < iCount; i++ {
        go func(rn int) {
            defer wg.Done()
            key := strconv.Itoa(rn)

            // you can prove the test works by commenting the locking out and seeing it fail
            l := m.Lock(key)
            defer l.Unlock()

            out <- key + " A"
            time.Sleep(time.Microsecond) // make 'em wait a mo'
            out <- key + " B"
        }(r.Intn(keyCount))
    }
    wg.Wait()
    close(out)

    // verify the map is empty now
    if l := len(m.ma); l != 0 {
        t.Errorf("unexpected map length at test end: %v", l)
    }

    // confirm that the output always produced the correct sequence
    outLists := make([][]string, keyCount)
    for s := range out {
        sParts := strings.Fields(s)
        kn, err := strconv.Atoi(sParts[0])
        if err != nil {
            t.Fatal(err)
        }
        outLists[kn] = append(outLists[kn], sParts[1])
    }
    for kn := 0; kn < keyCount; kn++ {
        l := outLists[kn] // list of output for this particular key
        for i := 0; i < len(l); i += 2 {
            if l[i] != "A" || l[i+1] != "B" {
                t.Errorf("For key=%v and i=%v got unexpected values %v and %v", kn, i, l[i], l[i+1])
                break
            }
        }
    }
    if t.Failed() {
        t.Logf("Failed, outLists: %#v", outLists)
    }

}

func BenchmarkM(b *testing.B) {

    m := New()

    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        // run uncontended lock/unlock - should be quite fast
        m.Lock(i).Unlock()
    }

}

答案 1 :(得分:1)

我写了一个简单的类似实现:mapmutex

但是,在此实现中,互斥体不是使用互斥体地图,而是使用互斥锁来保护地图,并且地图中的每个项目都像“锁定”一样使用。地图本身就是简单的普通地图。