Class: Lowdown::Threading::Counter
- Inherits:
-
Object
- Object
- Lowdown::Threading::Counter
- Defined in:
- lib/lowdown/threading.rb
Overview
A simple thread-safe counter.
Instance Method Summary collapse
-
#decrement! ⇒ void
Decrements the current count.
-
#increment! ⇒ void
Increments the current count.
-
#initialize(value = 0) ⇒ Counter
constructor
A new instance of Counter.
-
#value ⇒ Integer
The current count.
-
#value=(value) ⇒ Integer
The input value.
-
#zero? ⇒ Boolean
Whether or not the current count is zero.
Constructor Details
#initialize(value = 0) ⇒ Counter
Returns a new instance of Counter.
139 140 141 142 |
# File 'lib/lowdown/threading.rb', line 139 def initialize(value = 0) @value = value @mutex = Mutex.new end |
Instance Method Details
#decrement! ⇒ void
This method returns an undefined value.
Decrements the current count.
183 184 185 |
# File 'lib/lowdown/threading.rb', line 183 def decrement! @mutex.synchronize { @value -= 1 } end |
#increment! ⇒ void
This method returns an undefined value.
Increments the current count.
175 176 177 |
# File 'lib/lowdown/threading.rb', line 175 def increment! @mutex.synchronize { @value += 1 } end |
#value ⇒ Integer
Returns the current count.
147 148 149 150 151 |
# File 'lib/lowdown/threading.rb', line 147 def value value = nil @mutex.synchronize { value = @value } value end |
#value=(value) ⇒ Integer
Returns the input value.
166 167 168 169 |
# File 'lib/lowdown/threading.rb', line 166 def value=(value) @mutex.synchronize { @value = value } value end |
#zero? ⇒ Boolean
Returns whether or not the current count is zero.
156 157 158 |
# File 'lib/lowdown/threading.rb', line 156 def zero? value.zero? end |