https://github.com/yudai/umutex
Golang Unblocking Mutex
https://github.com/yudai/umutex
Last synced: 8 months ago
JSON representation
Golang Unblocking Mutex
- Host: GitHub
- URL: https://github.com/yudai/umutex
- Owner: yudai
- License: mit
- Created: 2015-08-09T07:11:14.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2015-08-17T08:01:37.000Z (about 10 years ago)
- Last Synced: 2024-12-28T13:18:09.131Z (9 months ago)
- Language: Go
- Size: 141 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Unblocking Mutex
This simple package provides unblocking mutexes for those who don't want to write many `select` clauses or get confused by numerous channels.
## Usage Example
```go
package mainimport (
"fmt"
"github.com/yudai/umutex"
)func main() {
// Create mutex
mutex := umutex.New()// First time, try should succeed
if mutex.TryLock() {
fmt.Println("SUCCESS")
} else {
fmt.Println("FAILURE")
}// Second time, try should fail as it's locked
if mutex.TryLock() {
fmt.Println("SUCCESS")
} else {
fmt.Println("FAILURE")
}// Unclock mutex
mutex.Unlock()// Third time, try should succeed again
if mutex.TryLock() {
fmt.Println("SUCCESS")
} else {
fmt.Println("FAILURE")
}
}
```The output is;
```sh
SUCCESS
FAILURE
SUCCESS
````ForceLock()` method is also availale for normal blocking lock.