Created
December 29, 2022 23:14
-
-
Save alexec/771aa9abeb293f9e2e1381f5ce1f47e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package proc | |
import "sync" | |
var locks = &sync.Map{} | |
// KeyLock return a mutex for the key. | |
// This func never frees un-locked mutexes. It is only suitable for use-cases with a small number of keys. | |
func KeyLock(key string) *sync.Mutex { | |
actual, _ := locks.LoadOrStore(key, &sync.Mutex{}) | |
mutex := actual.(*sync.Mutex) | |
return mutex | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment