Class: Ninja::Threaded::ThreadPool::Incrementor

Inherits:
Object
  • Object
show all
Defined in:
lib/ninja/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/ninja/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.



46
47
48
# File 'lib/ninja/threaded.rb', line 46

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

#inc(v = 1) ⇒ Object

Add the given value to self, default 1.



42
43
44
# File 'lib/ninja/threaded.rb', line 42

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

#inspectObject

Simply shows the value inspect for convenience.



50
51
52
# File 'lib/ninja/threaded.rb', line 50

def inspect
  @v.inspect
end

#to_iObject

Extract the value.



54
55
56
# File 'lib/ninja/threaded.rb', line 54

def to_i
  @v
end