converge/pkg/support/concurrency/atomiccounter.go
Erik Brakkee 538a697770 multiple clients connecting to multiple agents.
Clients cannot yet be started in parallel. due to subtle issue in test
setup with accept
2024-08-22 22:44:54 +02:00

24 lines
385 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) GetAndIncrement() int {
counter.mutex.Lock()
defer counter.mutex.Unlock()
val := counter.lastValue
counter.lastValue++
return val
}