Clients cannot yet be started in parallel. due to subtle issue in test setup with accept
24 lines
385 B
Go
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
|
|
}
|