Sha256: 2a125806bc61fc290e2780a52e1331813cb8d4d87a3cfcc6b748fa6552393629

Contents?: true

Size: 448 Bytes

Versions: 11

Compression:

Stored size: 448 Bytes

Contents

require 'thread'

# Maintains a thread-safe counter wrapped in a Mutex.
class Sqewer::AtomicCounter
  def initialize
    @m, @v = Mutex.new, 0
  end
  
  # Returns the current value of the counter
  #
  # @return [Fixnum] the current value of the counter
  def to_i
    @m.synchronize { @v + 0 }
  end
  
  # Increments the counter
  #
  # @return [Fixnum] the current value of the counter
  def increment!
    @m.synchronize { @v += 1 }
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
sqewer-5.0.0 lib/sqewer/atomic_counter.rb
sqewer-4.2.0 lib/sqewer/atomic_counter.rb
sqewer-4.1.0 lib/sqewer/atomic_counter.rb
sqewer-4.0.1 lib/sqewer/atomic_counter.rb
sqewer-4.0.0 lib/sqewer/atomic_counter.rb
sqewer-3.0.1 lib/sqewer/atomic_counter.rb
sqewer-3.0.0 lib/sqewer/atomic_counter.rb
sqewer-2.0.2 lib/sqewer/atomic_counter.rb
sqewer-2.0.1 lib/sqewer/atomic_counter.rb
sqewer-2.0.0 lib/sqewer/atomic_counter.rb
sqewer-1.0.0 lib/sqewer/atomic_counter.rb