Skip to content

AtomicRef

AtomicRef

AtomicRef(initial_value: T = None)

An object reference that may be updated atomically.

Methods:

  • compare_and_set

    Atomically read the current value of this AtomicRef:

  • get

    Atomically read the current value of this AtomicRef.

  • get_and_set

    Atomically swap the value of this AtomicRef to desired and return

  • get_handle

    Get a thread-local handle for this AtomicRef.

  • set

    Unconditionally set the value of this AtomicRef to desired.

compare_and_set

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

Atomically read the current value of this AtomicRef:

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

get

get() -> T

Atomically read the current value of this AtomicRef.

get_and_set

get_and_set(desired: T) -> T

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

get_handle

get_handle() -> ThreadHandle[Self]

Get a thread-local handle for this AtomicRef.

When using a thread-local handle, you can improve the performance of your application.

See ThreadHandle for more information on thread-local object handles.

set

set(desired: T)

Unconditionally set the value of this AtomicRef 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 AtomicRef.