TryLock support on read-write lock for Golang
go-trylock
implements sync.Locker
.
Have same interfaces with sync.RWMutex
Documentation can be found at Godoc
import (
"time"
"errors"
"github.com/subchen/go-trylock"
)
var mu = trylock.New()
func goroutineWrite() error {
if ok := mu.TryLock(1 * time.Second); !ok {
return errors.New("timeout, cannot TryLock !!!")
}
defer mu.Unlock()
// write something
}
func goroutineRead() {
if ok := mu.RTryLock(1 * time.Second); !ok {
return errors.New("timeout, cannot RTryLock !!!")
}
defer mu.RUnlock()
// read something
}
Apache 2.0