AtomicBool
AtomicBool
¶
A thread-safe boolean value.
Example
from cereggii import AtomicBool
flag = AtomicBool(False)
# atomically set the value
flag.set(True)
# get the current value
value = flag.get()
# compare and set if value matches expected
success = flag.compare_and_set(True, False)
# get the old value and set a new one atomically
old_value = flag.get_and_set(True)
Methods:
-
__init__–Initialize an
AtomicBoolwith the given value. -
compare_and_set–Atomically read the current value of this
AtomicBool: -
get–Atomically read the current value of this
AtomicBool. -
get_and_set–Atomically swap the value of this
AtomicBooltodesiredand return -
get_handle–Get a thread-local handle for this
AtomicBool. -
reset_or_raise–Reset the value of this
AtomicBooltoFalseif it is currentlyTrue, -
set–Unconditionally set the value of this
AtomicBooltodesired. -
set_or_raise–Set the value of this
AtomicBooltoTrueif it is currentlyFalse,
__init__
¶
__init__(initial_value: bool = False)
compare_and_set
¶
Atomically read the current value of this AtomicBool:
- if it is
expected, then replace it withdesiredand returnTrue - else, don't change it and return
False.
Raises:
-
TypeError–If expected or desired is not a bool.
get_and_set
¶
get_handle
¶
Get a thread-local handle for this AtomicBool.
When using a thread-local handle, you can improve the performance of your application.
See ThreadHandle for more
information on thread-local object handles.
reset_or_raise
¶
Reset the value of this AtomicBool to False if it is currently True,
or else raise an exception.
Raises:
-
ExpectationFailed–If the value is already
False.
set
¶
Unconditionally set the value of this AtomicBool to desired.
Warning
Use compare_and_set()
instead.
When using this method, it is not possible to know that the value
currently stored is the one being expected -- it may be mutated
by another thread before this mutation is applied. Use this method
only when no other thread may be writing to this AtomicBool.
Parameters:
Raises:
-
TypeError–If desired is not a bool.
set_or_raise
¶
Set the value of this AtomicBool to True if it is currently False,
or else raise an exception.
Raises:
-
ExpectationFailed–If the value is already
True.