class WaterDrop::Helpers::Counter

Atomic counter that we can safely increment and decrement without race conditions

Attributes

value[R]

@return [Integer] current value

Public Class Methods

new() click to toggle source
# File lib/waterdrop/helpers/counter.rb, line 11
def initialize
  @value = 0
  @mutex = Mutex.new
end

Public Instance Methods

decrement() click to toggle source

Decrements the value by 1

# File lib/waterdrop/helpers/counter.rb, line 22
def decrement
  @mutex.synchronize { @value -= 1 }
end
increment() click to toggle source

Increments the value by 1

# File lib/waterdrop/helpers/counter.rb, line 17
def increment
  @mutex.synchronize { @value += 1 }
end