Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fork of `github.com/neverlee/keymutex`

License

alxmsl/keymutex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fork of github.com/neverlee/keymutex

keymutex

A golang key set mutex. Supposing you have a set of keys describing resources that each need an independent mutex keymutex allows you to provide a pseudo-independent set of mutexes in constant size. Keymutex allocates a fixed sized array of mutexes to provide locking for an entire (potentially much larger) space of keys. This works by hashing the string key to provide an index into the backing array of mutexes. If keys collide in the hash function modulo the backing array size then they will share a mutex, meaning some indepedent keys will share a mutex but some will not. This allows you to trade space (size of mutex array) against mutex contention by chaning the size of the backing array.

Example

// Create a keymutex with backing array of 47 mutexes (prime number is good for hash coverage)
// Using a larger number here increases the memory usage by decreases the mutex contention.
var kmutex = keymutex.New(47)

// ...
go func(key string) {
    kmutex.Lock(key)
    defer kmutex.Unlock(key)
    // do something with the resource decribed by the key
}(akey)

About

Fork of `github.com/neverlee/keymutex`

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages