https://github.com/tsavola/lock
Go mutex helpers
https://github.com/tsavola/lock
go synchronization
Last synced: 9 months ago
JSON representation
Go mutex helpers
- Host: GitHub
- URL: https://github.com/tsavola/lock
- Owner: tsavola
- License: bsd-3-clause
- Created: 2021-12-05T19:04:02.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-10-06T00:04:26.000Z (about 1 year ago)
- Last Synced: 2025-03-11T02:09:34.980Z (9 months ago)
- Topics: go, synchronization
- Language: Go
- Homepage: https://import.name/lock
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### Example
```go
import "import.name/lock"
import "import.name/lock/unlock"
func ExampleLock() {
var (
mu sync.Mutex
count int
)
lock.Guard(&mu, func() {
count++
})
n := lock.Guarded(&mu, func() int {
return count
})
fmt.Println(n)
}
func ExampleUnlock() {
var mu sync.Mutex
mu.Lock()
defer mu.Unlock()
unlock.Guard(&mu, func() {
time.Sleep(time.Second)
})
}
func ExampleTagLock() {
type countLocked struct{}
var (
mu lock.TagMutex[countLocked]
count int
)
incrementWithLock := func(l countLocked) {
count++
}
readWithLock := func(l countLocked) int {
return count
}
lock.GuardTag(&mu, func(l countLocked) {
incrementWithLock(l)
})
n := lock.GuardTagged(&mu, func(l countLocked) int {
return readWithLock(l)
})
fmt.Println(n)
}
```