Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tsavola/lock
Go mutex helpers
https://github.com/tsavola/lock
go synchronization
Last synced: about 22 hours 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 (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-06T00:04:26.000Z (about 1 month ago)
- Last Synced: 2024-11-08T09:14:39.517Z (11 days ago)
- Topics: go, synchronization
- Language: Go
- Homepage: https://import.name/lock
- Size: 4.88 KB
- Stars: 0
- Watchers: 3
- 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.Mutexmu.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)
}
```