Class: Lowdown::Threading::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/lowdown/threading.rb

Overview

A simple thread-safe counter.

Instance Method Summary collapse

Constructor Details

#initialize(value = 0) ⇒ Counter

Returns a new instance of Counter.

Parameters:

  • value (Integer) (defaults to: 0)

    the initial count.



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

#valueInteger

Returns the current count.

Returns:

  • (Integer)

    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.

Parameters:

  • value (Integer)

    the new count.

Returns:

  • (Integer)

    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.

Returns:

  • (Boolean)

    whether or not the current count is zero.



156
157
158
# File 'lib/lowdown/threading.rb', line 156

def zero?
  value.zero?
end