Skip to content

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 AtomicBool with 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 AtomicBool to desired and return

  • get_handle

    Get a thread-local handle for this AtomicBool.

  • reset_or_raise

    Reset the value of this AtomicBool to False if it is currently True,

  • set

    Unconditionally set the value of this AtomicBool to desired.

  • set_or_raise

    Set the value of this AtomicBool to True if it is currently False,

__init__

__init__(initial_value: bool = False)

Initialize an AtomicBool with the given value.

Parameters:

  • initial_value

    (bool, default: False ) –

    The initial boolean value. Defaults to False.

compare_and_set

compare_and_set(expected: bool, desired: bool) -> bool

Atomically read the current value of this AtomicBool:

  • if it is expected, then replace it with desired and return True
  • else, don't change it and return False.

Raises:

  • TypeError

    If expected or desired is not a bool.

get

get() -> bool

Atomically read the current value of this AtomicBool.

get_and_set

get_and_set(desired: bool) -> bool

Atomically swap the value of this AtomicBool to desired and return the previously stored value.

Parameters:

  • desired

    (bool) –

    The new boolean value to set.

Returns:

  • bool

    The previous boolean value.

Raises:

  • TypeError

    If desired is not a bool.

get_handle

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_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

set(desired: bool)

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:

  • desired

    (bool) –

    The new boolean value to set.

Raises:

  • TypeError

    If desired is not a bool.

set_or_raise

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.