diff --git a/meter.go b/meter.go index 7807406..223669b 100644 --- a/meter.go +++ b/meter.go @@ -126,8 +126,6 @@ func (NilMeter) Stop() {} // StandardMeter is the standard implementation of a Meter. type StandardMeter struct { - // Only used on stop. - lock sync.Mutex snapshot *MeterSnapshot a1, a5, a15 EWMA startTime time.Time @@ -146,11 +144,7 @@ func newStandardMeter() *StandardMeter { // Stop stops the meter, Mark() will be a no-op if you use it after being stopped. func (m *StandardMeter) Stop() { - m.lock.Lock() - stopped := m.stopped - m.stopped = 1 - m.lock.Unlock() - if stopped != 1 { + if atomic.CompareAndSwapUint32(&m.stopped, 0, 1) { arbiter.Lock() delete(arbiter.meters, m) arbiter.Unlock() diff --git a/meter_test.go b/meter_test.go index 4235ca2..ecef37d 100644 --- a/meter_test.go +++ b/meter_test.go @@ -43,6 +43,11 @@ func TestMeterConcurrency(t *testing.T) { m.Mark(1) wg.Done() }(m, wg) + wg.Add(1) + go func(m Meter, wg *sync.WaitGroup) { + m.Stop() + wg.Done() + }(m, wg) } wg.Wait() }