Limit work
This commit is contained in:
parent
56ab34a57f
commit
42e46a7c22
9 changed files with 114 additions and 62 deletions
32
util/atomic_counter.go
Normal file
32
util/atomic_counter.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package util
|
||||
|
||||
import "sync"
|
||||
|
||||
type AtomicCounter[T int | int32 | int64] struct {
|
||||
value T
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewAtomicCounter[T int | int32 | int64](value T) *AtomicCounter[T] {
|
||||
return &AtomicCounter[T]{
|
||||
value: value,
|
||||
}
|
||||
}
|
||||
func (c *AtomicCounter[T]) Inc() T {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.value++
|
||||
return c.value
|
||||
}
|
||||
|
||||
func (c *AtomicCounter[T]) Value() T {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
return c.value
|
||||
}
|
||||
|
||||
func (c *AtomicCounter[T]) Reset() {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.value = 0
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue