Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thetardigrade/golang-namespacedmutex
This Go package enables mutexes to be accessed based on namespaces.
https://github.com/thetardigrade/golang-namespacedmutex
go golang mutex mutex-synchronisation mutual-exclusion namespace
Last synced: 4 days ago
JSON representation
This Go package enables mutexes to be accessed based on namespaces.
- Host: GitHub
- URL: https://github.com/thetardigrade/golang-namespacedmutex
- Owner: theTardigrade
- License: gpl-3.0
- Created: 2019-10-12T15:16:36.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2023-02-28T23:47:50.000Z (almost 2 years ago)
- Last Synced: 2024-11-08T11:13:38.967Z (about 2 months ago)
- Topics: go, golang, mutex, mutex-synchronisation, mutual-exclusion, namespace
- Language: Go
- Homepage:
- Size: 46.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# golang-namespacedMutex
This Go package enables a large number of mutexes to be stored and accessed based on namespaces.
[![Go Reference](https://pkg.go.dev/badge/github.com/theTardigrade/golang-namespacedMutex/.svg)](https://pkg.go.dev/github.com/theTardigrade/golang-namespacedMutex/) [![Go Report Card](https://goreportcard.com/badge/github.com/theTardigrade/golang-namespacedMutex)](https://goreportcard.com/report/github.com/theTardigrade/golang-namespacedMutex)
## Example
```golang
package mainimport (
"fmt"
"strconv"
"sync"namespacedMutex "github.com/theTardigrade/golang-namespacedMutex"
)var mutexManager = namespacedMutex.New(&namespacedMutex.Options{
BucketCount: 1 << 5,
BucketCountShouldBePrime: true,
})func main() {
numbers := make([]string, 0, 100)
var wg sync.WaitGroupwg.Add(cap(numbers))
for i := 1; i <= cap(numbers); i++ {
go func(i int) {
defer wg.Done()// when the Use function is called, a mutex stored
// under the namespace will automatically be locked
// before the handler function runs, and unlocked
// once it's finished
mutexManager.Use(false, "this-is-the-namespace", func() {
numbers = append(numbers, strconv.Itoa(i))
})
}(i)
}wg.Wait()
wg.Add(len(numbers))var numbersList string
for i := 0; i < len(numbers); i++ {
go func(i int) {
defer wg.Done()// you can also use a mutex directly by calling
// the GetLocked function
mutex := mutexManager.GetLocked(false, "another-namespace")
defer mutex.Unlock()numbersList += "(" + numbers[i] + ")"
}(i)
}wg.Wait()
fmt.Println(numbersList)
fmt.Println(len(numbers))
}
```## Support
If you use this package, or find any value in it, please consider donating:
[![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/S6S2EIRL0)