package agent import "sync/atomic" type Counter struct { atomic.Int32 } // Take will attempt to take an item from the counter. If the counter is zero, it will return false. func (c *Counter) Take() bool { for { current := c.Load() if current <= 0 { return false } if c.CompareAndSwap(current, current-1) { return true } } } // Return will return an item to the counter. func (c *Counter) Return() { c.Add(1) }