https://github.com/yudai/nmutex
Go Mutex With Names like a Map
https://github.com/yudai/nmutex
Last synced: 6 months ago
JSON representation
Go Mutex With Names like a Map
- Host: GitHub
- URL: https://github.com/yudai/nmutex
- Owner: yudai
- License: mit
- Created: 2016-09-29T05:20:25.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-09-29T05:23:10.000Z (about 9 years ago)
- Last Synced: 2025-04-12T16:17:21.212Z (6 months ago)
- Language: Go
- Size: 1.95 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Named Mutex
Named Mutex is a simple package that provides a mutex map.
## Instalation
```sh
go get -u github.com/yudai/nmutex
```## Example
```go
m := nmutex.New()releaseABC := m.Lock("abc") // you can get a lock
releaseXYZ := m.Lock("xyz") // you can get a lock with another name
defer releaseXYZ() // using defer to release locks is a best practicego func() {
// this call is blocked until releaseABC() has been called
releaseABC := m.Lock("abc")
defer releaseABC()
}()releaseABC() // release the first lock
// the goroutine above gets the lock at this timing
```When the lock for a name has been released and there is no other gouroutines waiting for the lock, the internal resource for the name is automatically released.