File: counter.go

package info (click to toggle)
golang-github-paulbellamy-ratecounter 0.2.0%2Bgit20170719.a803f0e-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 108 kB
  • sloc: makefile: 3
file content (21 lines) | stat: -rw-r--r-- 485 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package ratecounter

import "sync/atomic"

// A Counter is a thread-safe counter implementation
type Counter int64

// Incr method increments the counter by some value
func (c *Counter) Incr(val int64) {
	atomic.AddInt64((*int64)(c), val)
}

// Reset method resets the counter's value to zero
func (c *Counter) Reset() {
	atomic.StoreInt64((*int64)(c), 0)
}

// Value method returns the counter's current value
func (c *Counter) Value() int64 {
	return atomic.LoadInt64((*int64)(c))
}