converge/pkg/concurrency/atomiccounter.go

23 lines
373 B
Go

package concurrency
import "sync"
type AtomicCounter struct {
mutex sync.Mutex
lastValue int
}
func NewAtomicCounter() *AtomicCounter {
return &AtomicCounter{
mutex: sync.Mutex{},
lastValue: 0,
}
}
func (counter *AtomicCounter) IncrementAndGet() int {
counter.mutex.Lock()
defer counter.mutex.Unlock()
counter.lastValue++
return counter.lastValue
}