Bool
This class represents an atomic Boolean. It stores the boolean value as an unsigned 32-bit integer internally to leverage atomic functions available in Go.
Methods
Load()
@classmethod
def Load() - > bool
Fetches the current boolean value stored in the atomic boolean. Callers use this to retrieve the current state without modification.
Returns
| Type | Description |
|---|---|
bool | The current boolean value. |
CompareAndSwap()
@classmethod
def CompareAndSwap(
old: bool,
new: bool
) - > bool
Atomically compares the current boolean value with 'old' and, if they are equal, replaces the current value with 'new'. This is used for conditional updates, ensuring the value is changed only if it matches an expected state.
Parameters
| Name | Type | Description |
|---|---|---|
| old | bool | The expected current boolean value to compare against. |
| new | bool | The new boolean value to set if the comparison succeeds. |
Returns
| Type | Description |
|---|---|
bool | True if the swap occurred (i.e., the old value matched the current value), false otherwise. |
Store()
@classmethod
def Store(
new: bool
)
Atomically sets the boolean to the 'new' value. Callers use this to unconditionally update the boolean's state.
Parameters
| Name | Type | Description |
|---|---|---|
| new | bool | The boolean value to store. |
Swap()
@classmethod
def Swap(
new: bool
) - > bool
Atomically sets the boolean to the 'new' value and returns the old value. Callers use this to update the state and retrieve the previous state in a single atomic operation.
Parameters
| Name | Type | Description |
|---|---|---|
| new | bool | The new boolean value to set. |
Returns
| Type | Description |
|---|---|
bool | The boolean value that was previously stored before the swap. |
Toggle()
@classmethod
def Toggle() - > bool
Atomically inverts the current boolean value. Callers use this to flip the state (true to false, or false to true) in a thread-safe manner.
Returns
| Type | Description |
|---|---|
bool | The new boolean value after the toggle operation. |