Class: ThreadsafeSequence
- Inherits:
-
Object
- Object
- ThreadsafeSequence
- Defined in:
- lib/ruby-threading-toolkit/threadsafe_sequence.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(range = nil) ⇒ ThreadsafeSequence
constructor
A new instance of ThreadsafeSequence.
- #nextval ⇒ Object
Constructor Details
#initialize(range = nil) ⇒ ThreadsafeSequence
Returns a new instance of ThreadsafeSequence.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ruby-threading-toolkit/threadsafe_sequence.rb', line 18 def initialize(range = nil) if range @initial_value = @value = range.first - 1 @maximum = range.exclude_end? ? range.last - 1 : range.last else @initial_value = @value = 0 @maximum = nil end @mutex = Mutex.new end |
Instance Method Details
#nextval ⇒ Object
30 31 32 33 34 35 |
# File 'lib/ruby-threading-toolkit/threadsafe_sequence.rb', line 30 def nextval @mutex.synchronize do raise("Maximum sequence number achived (#{@maximum})") if @value == @maximum @value += 1 end end |