Class: Bob::Engine::Threaded::ThreadPool::Incrementor

Inherits:
Object
  • Object
show all
Defined in:
lib/bob/engine/threaded.rb

Overview

A thread safe single value for use as a counter.

Instance Method Summary collapse

Constructor Details

#initialize(v = 0) ⇒ Incrementor

The value passed in will be the initial value.



37
38
39
40
# File 'lib/bob/engine/threaded.rb', line 37

def initialize(v = 0)
  @m = Mutex.new
  @v = v
end

Instance Method Details

#dec(v = 1) ⇒ Object

Subtract the given value to self, default 1.



48
49
50
# File 'lib/bob/engine/threaded.rb', line 48

def dec(v = 1)
  sync { @v -= v }
end

#inc(v = 1) ⇒ Object

Add the given value to self, default 1.



43
44
45
# File 'lib/bob/engine/threaded.rb', line 43

def inc(v = 1)
  sync { @v += v }
end

#inspectObject

Simply shows the value inspect for convenience.



53
54
55
# File 'lib/bob/engine/threaded.rb', line 53

def inspect
  @v.inspect
end

#to_iObject

Extract the value.



58
59
60
# File 'lib/bob/engine/threaded.rb', line 58

def to_i
  @v
end