class Redstruct::Utils::AtomicCounter

Very basic utility class to have thread-safe counters

Public Class Methods

new(initial = 0) click to toggle source

@param [Integer] initial the initial value of the counter

# File lib/redstruct/utils/atomic_counter.rb, line 8
def initialize(initial = 0)
  @lock = Mutex.new
  @current = initial
end

Public Instance Methods

decrement(by: 1) click to toggle source

Decrements the counter by the given delta @param [Integer] by the delta to decrement by @return [Integer] the new, decremented value

# File lib/redstruct/utils/atomic_counter.rb, line 25
def decrement(by: 1)
  return increment(by: -by.to_i)
end
increment(by: 1) click to toggle source

Increments the counter by the given delta @param [Integer] by the delta to increment by @return [Integer] the new, incremented value

# File lib/redstruct/utils/atomic_counter.rb, line 16
def increment(by: 1)
  return @lock.synchronize do
    @current += by.to_i
  end
end