https://github.com/hlts2/gpin
A spinlock implementation for Go.
https://github.com/hlts2/gpin
faster go go-library golang golang-library goroutine-safe lock simple
Last synced: 2 months ago
JSON representation
A spinlock implementation for Go.
- Host: GitHub
- URL: https://github.com/hlts2/gpin
- Owner: hlts2
- License: mit
- Created: 2021-12-16T15:38:54.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-12-20T01:35:59.000Z (over 3 years ago)
- Last Synced: 2025-01-20T08:09:33.159Z (4 months ago)
- Topics: faster, go, go-library, golang, golang-library, goroutine-safe, lock, simple
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gpin
A [spinlock](https://en.wikipedia.org/wiki/Spinlock) implementation for Go.## Requirement
Go 1.17
## Installation
```shell
go get github.com/hlts2/gpin
```## Example
```go
package mainimport (
"fmt"
"time""github.com/hlts2/gpin"
)type Counter struct {
l gpin.Spinlock
v map[string]int
}func (c *Counter) Increment(key string) {
c.l.Lock()
c.v[key]++
c.l.Unlock()
}func (c *Counter) Get(key string) int {
c.l.Lock()
defer c.l.Unlock()
return c.v[key]
}func main() {
c := Counter{
v: make(map[string]int),
}
for i := 0; i < 100; i++ {
go c.Increment("example")
}time.Sleep(time.Second)
fmt.Println(c.Get("example")) // 100
}```
## Benchmark
```
goos: linux
goarch: amd64
pkg: github.com/hlts2/gpin
cpu: 11th Gen Intel(R) Core(TM) i9-11900K @ 3.50GHz
BenchmarkMutex
BenchmarkMutex-16 10193804 135.3 ns/op 0 B/op 0 allocs/op
BenchmarkSpinlock
BenchmarkSpinlock-16 43065172 30.98 ns/op 0 B/op 0 allocs/op
```