Class: Threadz::AtomicInteger
- Inherits:
-
Object
- Object
- Threadz::AtomicInteger
- Defined in:
- lib/threadz/atomic_integer.rb
Instance Method Summary collapse
- #decrement(amount = 1) ⇒ Object
- #increment(amount = 1) ⇒ Object
-
#initialize(value) ⇒ AtomicInteger
constructor
A new instance of AtomicInteger.
- #value ⇒ Object
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 |
#value ⇒ Object
10 11 12 |
# File 'lib/threadz/atomic_integer.rb', line 10 def value @value end |