class Concurrent::Tuple
@see ruby-doc.org/core-2.2.2/Enumerable.html Enumerable
@see www.erlang.org/doc/reference_manual/data_types.html#id70396 Erlang Tuple
@see en.wikipedia.org/wiki/Tuple Tuple entry at Wikipedia
tuple.get(0) #=> :bar | volatile read
tuple.cas(0, :foo, :baz) #=> false | strong CAS
tuple.compare_and_set(0, :foo, :bar) #=> true | strong CAS
tuple.get(0) #=> :foo | volatile read
tuple.set(0, :foo) #=> :foo | volatile write
tuple = Concurrent::Tuple.new(16)
@example
Mixes in Ruby’s ‘Enumerable` module for enhanced search, sort, and traversal.
A fixed size array with volatile (synchronized, thread safe) getters/setters.
def compare_and_set(i, old_value, new_value)
-
(Boolean)
- true if the value at the given element was set else false
Parameters:
-
new_value
(Object
) -- the value to set at the given index -
old_value
(Object
) -- the value to compare against the current value -
i
(Integer
) -- the index for the element to set
def compare_and_set(i, old_value, new_value) return false if i >= @size || i < 0 @tuple[i].compare_and_set(old_value, new_value) end
def each
- Yieldparam: ref - the `Concurrent::AtomicReference` object at the current index
def each @tuple.each {|ref| yield ref.get} end
def get(i)
-
(Object)
- the value at the given index or nil if the index is out of bounds
Parameters:
-
i
(Integer
) -- the index from which to retrieve the value
def get(i) return nil if i >= @size || i < 0 @tuple[i].get end
def initialize(size)
-
size
(Integer
) -- the number of elements in the tuple
def initialize(size) @size = size @tuple = tuple = ::Array.new(size) i = 0 while i < size tuple[i] = Concurrent::AtomicReference.new i += 1 end end
def set(i, value)
-
(Object)
- the new value of the element at the given index or nil if the index is out of bounds
Parameters:
-
value
(Object
) -- the value to set at the given index -
i
(Integer
) -- the index for the element to set
def set(i, value) return nil if i >= @size || i < 0 @tuple[i].set(value) end