Class: WaterDrop::Helpers::Counter
- Inherits:
-
Object
- Object
- WaterDrop::Helpers::Counter
- Defined in:
- lib/waterdrop/helpers/counter.rb
Overview
Atomic counter that we can safely increment and decrement without race conditions
Instance Attribute Summary collapse
-
#value ⇒ Integer
readonly
Current value.
Instance Method Summary collapse
-
#decrement ⇒ Object
Decrements the value by 1.
-
#increment ⇒ Object
Increments the value by 1.
-
#initialize ⇒ Counter
constructor
A new instance of Counter.
Constructor Details
#initialize ⇒ Counter
Returns a new instance of Counter.
11 12 13 14 |
# File 'lib/waterdrop/helpers/counter.rb', line 11 def initialize @value = 0 @mutex = Mutex.new end |
Instance Attribute Details
#value ⇒ Integer (readonly)
Returns current value.
9 10 11 |
# File 'lib/waterdrop/helpers/counter.rb', line 9 def value @value end |
Instance Method Details
#decrement ⇒ Object
Decrements the value by 1
22 23 24 |
# File 'lib/waterdrop/helpers/counter.rb', line 22 def decrement @mutex.synchronize { @value -= 1 } end |
#increment ⇒ Object
Increments the value by 1
17 18 19 |
# File 'lib/waterdrop/helpers/counter.rb', line 17 def increment @mutex.synchronize { @value += 1 } end |