go-trylock
TryLock support on read-write lock for Golang
About go-trylock
go-trylock
TryLock support on read-write lock for Golang
Interface
go-trylock implements sync.Locker.
Have same interfaces with sync.RWMutex
Documentation can be found at Godoc
Examples
import (
"context"
"time"
"errors"
"github.com/subchen/go-trylock/v2"
)
var mu = trylock.New()
func goroutineWrite() error {
if ok := mu.TryLock(context.Background()); !ok {
return errors.New("timeout, cannot TryLock !!!")
}
defer mu.Unlock()
// write something
}
func goroutineWriteTimeout() error {
if ok := mu.TryLockTimeout(1 * time.Second); !ok {
return errors.New("timeout, cannot TryLock !!!")
}
defer mu.Unlock()
// write something
}
func goroutineRead() {
if ok := mu.RTryLock(context.Background()); !ok {
return errors.New("timeout, cannot RTryLock !!!")
}
defer mu.RUnlock()
// read something
}
func goroutineReadTimeout() {
if ok := mu.RTryLockTimeout(1 * time.Second); !ok {
return errors.New("timeout, cannot RTryLock !!!")
}
defer mu.RUnlock()
// read something
}
LICENSE
Apache 2.0
Frequently Asked Questions
What is go-trylock?
go-trylock is a Goroutines library for the Go programming language. TryLock support on read-write lock for Golang
How do I install go-trylock?
Install go-trylock with the Go module system using `go get subchen/go-trylock`. Check the repository for the current installation instructions.
What category does go-trylock belong to?
go-trylock is listed under Goroutines, specifically Goroutines.