Mojo struct

# Atomic

Represents a value with atomic operations.

The class provides atomic `add`

and `sub`

methods for mutating the value.

## Parametersβ

- β
**type**(`DType`

): DType of the value.

## Fieldsβ

- β
**value**(`SIMD[type, 1]`

): The atomic value.

## Implemented traitsβ

`AnyType`

## Methodsβ

`__init__`

β

`__init__(inout self: Self, value: SIMD[type, 1])`

Constructs a new atomic value.

**Args:**

- β
**value**(`SIMD[type, 1]`

): Initial value represented as`Scalar[type]`

type.

`__iadd__`

β

`__iadd__(inout self: Self, rhs: SIMD[type, 1])`

Performs atomic in-place add.

Atomically replaces the current value with the result of arithmetic addition of the value and arg. That is, it performs atomic post-increment. The operation is a read-modify-write operation. Memory is affected according to the value of order which is sequentially consistent.

**Args:**

- β
**rhs**(`SIMD[type, 1]`

): Value to add.

`__isub__`

β

`__isub__(inout self: Self, rhs: SIMD[type, 1])`

Performs atomic in-place sub.

Atomically replaces the current value with the result of arithmetic subtraction of the value and arg. That is, it performs atomic post-decrement. The operation is a read-modify-write operation. Memory is affected according to the value of order which is sequentially consistent.

**Args:**

- β
**rhs**(`SIMD[type, 1]`

): Value to subtract.

`load`

β

`load(inout self: Self) -> SIMD[type, 1]`

Loads the current value from the atomic.

**Returns:**

The current value of the atomic.

`fetch_add`

β

`fetch_add(inout self: Self, rhs: SIMD[type, 1]) -> SIMD[type, 1]`

Performs atomic in-place add.

Atomically replaces the current value with the result of arithmetic addition of the value and arg. That is, it performs atomic post-increment. The operation is a read-modify-write operation. Memory is affected according to the value of order which is sequentially consistent.

**Args:**

- β
**rhs**(`SIMD[type, 1]`

): Value to add.

**Returns:**

The original value before addition.

`fetch_sub`

β

`fetch_sub(inout self: Self, rhs: SIMD[type, 1]) -> SIMD[type, 1]`

Performs atomic in-place sub.

Atomically replaces the current value with the result of arithmetic subtraction of the value and arg. That is, it performs atomic post-decrement. The operation is a read-modify-write operation. Memory is affected according to the value of order which is sequentially consistent.

**Args:**

- β
**rhs**(`SIMD[type, 1]`

): Value to subtract.

**Returns:**

The original value before subtraction.

`compare_exchange_weak`

β

`compare_exchange_weak(inout self: Self, inout expected: SIMD[type, 1], desired: SIMD[type, 1]) -> Bool`

Atomically compares the self value with that of the expected value. If the values are equal, then the self value is replaced with the desired value and True is returned. Otherwise, False is returned the the expected value is rewritten with the self value.

**Args:**

- β
**expected**(`SIMD[type, 1]`

): The expected value. - β
**desired**(`SIMD[type, 1]`

): The desired value.

**Returns:**

True if self == expected and False otherwise.

`max`

β

`static max(ptr: UnsafePointer[SIMD[type, 1], address_space, exclusive, alignment], rhs: SIMD[type, 1])`

Performs atomic in-place max on the pointer.

Atomically replaces the current value pointer to by `ptr`

by the result
of max of the value and arg. The operation is a read-modify-write
operation. The operation is a read-modify-write operation perform
according to sequential consistency semantics.

**Constraints:**

The input type must be either integral or floating-point type.

**Args:**

- β
**ptr**(`UnsafePointer[SIMD[type, 1], address_space, exclusive, alignment]`

): The source pointer. - β
**rhs**(`SIMD[type, 1]`

): Value to max.

`max(inout self: Self, rhs: SIMD[type, 1])`

Performs atomic in-place max.

Atomically replaces the current value with the result of max of the value and arg. The operation is a read-modify-write operation perform according to sequential consistency semantics.

**Constraints:**

The input type must be either integral or floating-point type.

**Args:**

- β
**rhs**(`SIMD[type, 1]`

): Value to max.

`min`

β

`static min(ptr: UnsafePointer[SIMD[type, 1], address_space, exclusive, alignment], rhs: SIMD[type, 1])`

Performs atomic in-place min on the pointer.

Atomically replaces the current value pointer to by `ptr`

by the result
of min of the value and arg. The operation is a read-modify-write
operation. The operation is a read-modify-write operation perform
according to sequential consistency semantics.

**Constraints:**

The input type must be either integral or floating-point type.

**Args:**

- β
**ptr**(`UnsafePointer[SIMD[type, 1], address_space, exclusive, alignment]`

): The source pointer. - β
**rhs**(`SIMD[type, 1]`

): Value to min.

`min(inout self: Self, rhs: SIMD[type, 1])`

Performs atomic in-place min.

Atomically replaces the current value with the result of min of the value and arg. The operation is a read-modify-write operation. The operation is a read-modify-write operation perform according to sequential consistency semantics.

**Constraints:**

The input type must be either integral or floating-point type.

**Args:**

- β
**rhs**(`SIMD[type, 1]`

): Value to min.

Was this page helpful?

Thank you! We'll create more content like this.

Thank you for helping us improve!

If you'd like to share more information, please report an issue on GitHub

π What went wrong?