Class: Sqewer::AtomicCounter
- Inherits:
-
Object
- Object
- Sqewer::AtomicCounter
- Defined in:
- lib/sqewer/atomic_counter.rb
Overview
Maintains a thread-safe counter wrapped in a Mutex.
Instance Method Summary collapse
-
#increment! ⇒ Fixnum
Increments the counter.
-
#initialize ⇒ AtomicCounter
constructor
A new instance of AtomicCounter.
-
#to_i ⇒ Fixnum
Returns the current value of the counter.
Constructor Details
#initialize ⇒ AtomicCounter
Returns a new instance of AtomicCounter.
5 6 7 |
# File 'lib/sqewer/atomic_counter.rb', line 5 def initialize @m, @v = Mutex.new, 0 end |
Instance Method Details
#increment! ⇒ Fixnum
Increments the counter
19 20 21 |
# File 'lib/sqewer/atomic_counter.rb', line 19 def increment! @m.synchronize { @v += 1 } end |
#to_i ⇒ Fixnum
Returns the current value of the counter
12 13 14 |
# File 'lib/sqewer/atomic_counter.rb', line 12 def to_i @m.synchronize { @v + 0 } end |