struct Atomic(T)
Overview
A value that may be updated atomically.
Only primitive integer types, reference types or nilable reference types can be used with an Atomic type.
Defined in:
atomic.crConstructors
-
.new(value : T)
Creates an Atomic with the given initial value.
Instance Method Summary
-
#add(value : T)
Performs
atomic_value += value
. -
#and(value : T)
Performs
atomic_value &= value
. -
#compare_and_set(cmp : T, new : T) : Tuple(T, Bool)
Compares this atomic's value with cmp:
-
#get
Atomically returns this atomic's value.
-
#lazy_get
Non-atomically returns this atomic's value.
-
#lazy_set(value : T)
Non-atomically sets this atomic's value to value.
-
#max(value : T)
Performs
atomic_value = max(atomic_value, value)
. -
#min(value : T)
Performs
atomic_value = min(atomic_value, value)
. -
#nand(value : T)
Performs
atomic_value = ~(atomic_value & value)
. -
#or(value : T)
Performs
atomic_value |= value
. -
#set(value : T)
Atomically sets this atomic's value to value.
-
#sub(value : T)
Performs
atomic_value -= value
. -
#swap(value : T)
Atomically sets this atomic's value to value.
-
#xor(value : T)
Performs
atomic_value ^= value
.
Instance methods inherited from struct Struct
==(other : self) : Bool
==,
hash : Int32
hash,
inspect(io : IO) : Nil
inspect,
pretty_print(pp) : Nil
pretty_print,
to_s(io)
to_s
Instance methods inherited from struct Value
==(other)
==,
dup
dup
Instance methods inherited from class Object
!=(other)
!=,
!~(other)
!~,
==(other)
==,
===(other : JSON::Any)===(other : YAML::Any)
===(other) ===, =~(other) =~, class class, dup dup, hash hash, inspect(io : IO)
inspect inspect, itself itself, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, tap(&block) tap, to_json(io : IO)
to_json to_json, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ") to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml(io : IO)
to_yaml to_yaml, try(&block) try, unsafe_as(type : T.class) forall T unsafe_as
Constructor methods inherited from class Object
from_json(string_or_io, root : String) : selffrom_json(string_or_io) : self from_json, from_yaml(string_or_io) : self from_yaml
Constructor Detail
Instance Method Detail
Performs atomic_value += value
. Returns the old value.
atomic = Atomic.new(1)
atomic.add(2) # => 1
atomic.get # => 3
Performs atomic_value &= value
. Returns the old value.
atomic = Atomic.new(5)
atomic.and(3) # => 5
atomic.get # => 1
Compares this atomic's value with cmp:
- if they are equal, sets the value to new, and returns
{old_value, true}
- if they are not equal the value remains the same, and returns
{old_value, false}
atomic = Atomic.new(1)
atomic.compare_and_set(2, 3) # => {1, false}
atomic.get # => 1
atomic.compare_and_set(1, 3) # => {1, true}
atomic.get # => 3
Non-atomically sets this atomic's value to value. Returns the new value.
atomic = Atomic.new(5)
atomic.lazy_set(10) # => 10
atomic.get # => 10
Performs atomic_value = max(atomic_value, value)
. Returns the old value.
atomic = Atomic.new(5)
atomic.max(3) # => 5
atomic.get # => 5
atomic.max(10) # => 5
atomic.get # => 10
Performs atomic_value = min(atomic_value, value)
. Returns the old value.
atomic = Atomic.new(5)
atomic.min(10) # => 5
atomic.get # => 5
atomic.min(3) # => 5
atomic.get # => 3
Performs atomic_value = ~(atomic_value & value)
. Returns the old value.
atomic = Atomic.new(5)
atomic.nand(3) # => 5
atomic.get # => -2
Performs atomic_value |= value
. Returns the old value.
atomic = Atomic.new(5)
atomic.or(2) # => 5
atomic.get # => 7
Atomically sets this atomic's value to value. Returns the new value.
atomic = Atomic.new(5)
atomic.set(10) # => 10
atomic.get # => 10
Performs atomic_value -= value
. Returns the old value.
atomic = Atomic.new(9)
atomic.sub(2) # => 9
atomic.get # => 7
Atomically sets this atomic's value to value. Returns the old value.
atomic = Atomic.new(5)
atomic.swap(10) # => 5
atomic.get # => 10
Performs atomic_value ^= value
. Returns the old value.
atomic = Atomic.new(5)
atomic.xor(3) # => 5
atomic.get # => 6