Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
subchen authored Dec 26, 2019
1 parent e6ea8fd commit 0fd220b
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,25 @@ Documentation can be found at [Godoc](https://godoc.org/github.com/subchen/go-tr

```go
import (
"context"
"time"
"errors"
"github.com/subchen/go-trylock"
"github.com/subchen/go-trylock/v2"
)

var mu = trylock.New()

func goroutineWrite() error {
if ok := mu.TryLock(1 * time.Second); !ok {
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()
Expand All @@ -37,7 +47,16 @@ func goroutineWrite() error {
}

func goroutineRead() {
if ok := mu.RTryLock(1 * time.Second); !ok {
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()
Expand Down

0 comments on commit 0fd220b

Please sign in to comment.