background/vendor/git.jiaxianghudong.com/go/utils/counter.go

25 lines
317 B
Go
Raw Normal View History

2019-01-28 03:19:49 +00:00
package utils
import (
"sync/atomic"
)
// Counter 原子计数器
type Counter struct {
v int64
}
// Add 计数加
func (c *Counter) Add(i int64) {
atomic.AddInt64(&c.v, i)
}
// Get 取计数
func (c *Counter) Get() int64 {
return c.v
}
// Reset 重置计数器
func (c *Counter) Reset() {
c.Add(c.v * -1)
}