Class: Sqewer::AtomicCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/sqewer/atomic_counter.rb

Overview

Maintains a thread-safe counter wrapped in a Mutex.

Instance Method Summary collapse

Constructor Details

#initializeAtomicCounter

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

Returns:

  • (Fixnum)

    the current value of the counter



19
20
21
# File 'lib/sqewer/atomic_counter.rb', line 19

def increment!
  @m.synchronize { @v += 1 }
end

#to_iFixnum

Returns the current value of the counter

Returns:

  • (Fixnum)

    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