Class: Threadz::AtomicInteger

Inherits:
Object
  • Object
show all
Defined in:
lib/threadz/atomic_integer.rb

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ AtomicInteger

Returns a new instance of AtomicInteger.



5
6
7
8
# File 'lib/threadz/atomic_integer.rb', line 5

def initialize(value)
	@value = value
	@mutex = Mutex.new
end

Instance Method Details

#decrement(amount = 1) ⇒ Object



22
23
24
25
26
# File 'lib/threadz/atomic_integer.rb', line 22

def decrement(amount=1)
	@mutex.lock
	@value -= amount
	@mutex.unlock
end

#increment(amount = 1) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/threadz/atomic_integer.rb', line 14

def increment(amount=1)
	# We could use Mutex#synchronize here, but compared to modifying an
	# integer, creating a block is crazy expensive
	@mutex.lock
	@value += amount
	@mutex.unlock
end

#valueObject



10
11
12
# File 'lib/threadz/atomic_integer.rb', line 10

def value
	@value
end