Class: Baykit::BayServer::Util::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/baykit/bayserver/util/counter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init = 1) ⇒ Counter

Returns a new instance of Counter.



8
9
10
11
# File 'lib/baykit/bayserver/util/counter.rb', line 8

def initialize(init=1)
  @counter = init
  @mutex = Mutex.new
end

Instance Attribute Details

#counterObject (readonly)

Returns the value of attribute counter.



5
6
7
# File 'lib/baykit/bayserver/util/counter.rb', line 5

def counter
  @counter
end

#mutexObject (readonly)

Returns the value of attribute mutex.



6
7
8
# File 'lib/baykit/bayserver/util/counter.rb', line 6

def mutex
  @mutex
end

Instance Method Details

#nextObject



13
14
15
16
17
18
19
# File 'lib/baykit/bayserver/util/counter.rb', line 13

def next
  @mutex.synchronize do
    c = @counter
    @counter += 1
    c
  end
end