Show / Hide Table of Contents

Class Atomic

Exposes atomic operations for thread-safe scenarios.

Inheritance
object
Atomic
Inherited Members
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: DotNext.Threading
Assembly: DotNext.dll
Syntax
public static class Atomic

Methods

View Source

AccumulateAndGet<T>(ref T, T, Func<T, T, T>)

Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value.

Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static T AccumulateAndGet<T>(ref T location, T x, Func<T, T, T> accumulator)
Parameters
Type Name Description
T location

Reference to a value to be modified.

T x

Accumulator operand.

Func<T, T, T> accumulator

A side-effect-free function of two arguments.

Returns
Type Description
T

The updated value.

Type Parameters
Name Description
T
Remarks

The function is applied with the current value as its first argument, and the given update as the second argument.

View Source

AccumulateAndGet<T, TAccumulator>(ref T, T, TAccumulator)

Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value.

Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static T AccumulateAndGet<T, TAccumulator>(ref T location, T x, TAccumulator accumulator) where TAccumulator : ISupplier<T, T, T>, allows ref struct
Parameters
Type Name Description
T location

Reference to a value to be modified.

T x

Accumulator operand.

TAccumulator accumulator

A side-effect-free function of two arguments.

Returns
Type Description
T

The updated value.

Type Parameters
Name Description
T
TAccumulator

The type implementing accumulator.

Remarks

The function is applied with the current value as its first argument, and the given update as the second argument.

View Source

FalseToTrue(ref bool)

Atomically sets true value if the current value is false.

Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static bool FalseToTrue(ref bool location)
Parameters
Type Name Description
bool location

The destination, whose value is compared with comparand and possibly replaced.

Returns
Type Description
bool

true if current value is modified successfully; otherwise, false.

View Source

GetAndAccumulate<T>(ref T, T, Func<T, T, T>)

Atomically updates the current value with the results of applying the given function to the current and given values, returning the original value.

Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static T GetAndAccumulate<T>(ref T location, T x, Func<T, T, T> accumulator)
Parameters
Type Name Description
T location

Reference to a value to be modified.

T x

Accumulator operand.

Func<T, T, T> accumulator

A side-effect-free function of two arguments.

Returns
Type Description
T

The original value.

Type Parameters
Name Description
T
Remarks

The function is applied with the current value as its first argument, and the given update as the second argument.

View Source

GetAndAccumulate<T, TAccumulator>(ref T, T, TAccumulator)

Atomically updates the current value with the results of applying the given function to the current and given values, returning the original value.

Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static T GetAndAccumulate<T, TAccumulator>(ref T location, T x, TAccumulator accumulator) where TAccumulator : ISupplier<T, T, T>, allows ref struct
Parameters
Type Name Description
T location

Reference to a value to be modified.

T x

Accumulator operand.

TAccumulator accumulator

A side-effect-free function of two arguments.

Returns
Type Description
T

The original value.

Type Parameters
Name Description
T
TAccumulator

The type implementing accumulator.

Remarks

The function is applied with the current value as its first argument, and the given update as the second argument.

View Source

GetAndNegate(ref bool)

Negates currently stored value atomically.

Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static bool GetAndNegate(ref bool location)
Parameters
Type Name Description
bool location

Reference to a value to be modified.

Returns
Type Description
bool

The original value before negation.

View Source

GetAndUpdate<T>(ref T, Func<T, T>)

Atomically updates the stored value with the results of applying the given function, returning the original value.

Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static T GetAndUpdate<T>(ref T location, Func<T, T> updater)
Parameters
Type Name Description
T location

Reference to a value to be modified.

Func<T, T> updater

A side-effect-free function.

Returns
Type Description
T

The original value.

Type Parameters
Name Description
T
View Source

GetAndUpdate<T, TUpdater>(ref T, TUpdater)

Atomically updates the stored value with the results of applying the given function, returning the original value.

Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static T GetAndUpdate<T, TUpdater>(ref T location, TUpdater updater) where TUpdater : ISupplier<T, T>, allows ref struct
Parameters
Type Name Description
T location

Reference to a value to be modified.

TUpdater updater

A side-effect-free function.

Returns
Type Description
T

The original value.

Type Parameters
Name Description
T
TUpdater

The type implementing updater.

View Source

GetOrSet<T>(ref Atomic<Optional<T>>, T, out bool)

Returns the value if it is defined; or sets the supplied value.

Declaration
[MemoryOrdering(MemoryOrdering.Acquire)]
public static T GetOrSet<T>(this ref Atomic<Optional<T>> atomic, T value, out bool isSet)
Parameters
Type Name Description
Atomic<Optional<T>> atomic
T value

The value to set if the receiver is undefined.

bool isSet

true if value is written to the container; false if the receiver already has a value and value is ignored.

Returns
Type Description
T

The existing value contained in the receiver; or value.

Type Parameters
Name Description
T
View Source

IsAtomic<T>()

Determines that write to the location in the memory of type T is atomic.

Declaration
public static bool IsAtomic<T>()
Returns
Type Description
bool

true if write is atomic; otherwise, false.

Type Parameters
Name Description
T
View Source

NegateAndGet(ref bool)

Negates currently stored value atomically.

Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static bool NegateAndGet(ref bool location)
Parameters
Type Name Description
bool location

Reference to a value to be modified.

Returns
Type Description
bool

Negation result.

View Source

Read(ref readonly double)

Reads atomically the value from the specified location in the memory.

Declaration
[CLSCompliant(false)]
[MemoryOrdering(MemoryOrdering.Acquire)]
public static double Read(ref readonly double location)
Parameters
Type Name Description
double location

The location of the value.

Returns
Type Description
double

The value at the specified location.

Remarks

This method works correctly on 32-bit and 64-bit architectures.

View Source

Read(ref readonly long)

Reads atomically the value from the specified location in the memory.

Declaration
[MemoryOrdering(MemoryOrdering.Acquire)]
public static long Read(ref readonly long location)
Parameters
Type Name Description
long location

The location of the value.

Returns
Type Description
long

The value at the specified location.

Remarks

This method works correctly on 32-bit and 64-bit architectures.

View Source

Read(ref readonly ulong)

Reads atomically the value from the specified location in the memory.

Declaration
[CLSCompliant(false)]
[MemoryOrdering(MemoryOrdering.Acquire)]
public static ulong Read(ref readonly ulong location)
Parameters
Type Name Description
ulong location

The location of the value.

Returns
Type Description
ulong

The value at the specified location.

Remarks

This method works correctly on 32-bit and 64-bit architectures.

View Source

Read<T>(T**)

Reads the pointer from the specified location. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: if a read or write appears after this method in the code, the processor cannot move it before this method.

Declaration
[CLSCompliant(false)]
[MemoryOrdering(MemoryOrdering.Acquire)]
public static T* Read<T>(T** location) where T : unmanaged
Parameters
Type Name Description
T** location

The location of the pointer to read.

Returns
Type Description
T*

The pointer that was read.

Type Parameters
Name Description
T

The type of the value referenced by the pointer.

Remarks

Unlike volatile field modifier, this method can be used for the location obtained through the pointer.

View Source

Read<T>(ref readonly T*)

Reads the pointer from the specified location. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: if a read or write appears after this method in the code, the processor cannot move it before this method.

Declaration
[CLSCompliant(false)]
[MemoryOrdering(MemoryOrdering.Acquire)]
public static T* Read<T>(ref readonly T* location) where T : unmanaged
Parameters
Type Name Description
T* location

The location of the pointer to read.

Returns
Type Description
T*

The pointer that was read.

Type Parameters
Name Description
T

The type of the value referenced by the pointer.

Remarks

Unlike volatile field modifier, this method can be used for any location passed by reference, such as an array element or a field of the struct accessed by reference.

View Source

TrueToFalse(ref bool)

Atomically sets false value if the current value is true.

Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static bool TrueToFalse(ref bool location)
Parameters
Type Name Description
bool location

The destination, whose value is compared with comparand and possibly replaced.

Returns
Type Description
bool

true if current value is modified successfully; otherwise, false.

View Source

TrySet<T>(ref Atomic<Optional<T>>, T)

Sets the value to if it's not defined in the receiver.

Declaration
[MemoryOrdering(MemoryOrdering.Acquire)]
public static bool TrySet<T>(this ref Atomic<Optional<T>> atomic, T value)
Parameters
Type Name Description
Atomic<Optional<T>> atomic
T value

The value to set.

Returns
Type Description
bool

true if value is set to the empty receiver; otherwise, false.

Type Parameters
Name Description
T
View Source

UpdateAndGet<T>(ref T, Func<T, T>)

Atomically updates the stored value with the results of applying the given function, returning the updated value.

Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static T UpdateAndGet<T>(ref T location, Func<T, T> updater)
Parameters
Type Name Description
T location

Reference to a value to be modified.

Func<T, T> updater

A side-effect-free function.

Returns
Type Description
T

The updated value.

Type Parameters
Name Description
T
View Source

UpdateAndGet<T, TUpdater>(ref T, TUpdater)

Atomically updates the stored value with the results of applying the given function, returning the updated value.

Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static T UpdateAndGet<T, TUpdater>(ref T location, TUpdater updater) where TUpdater : ISupplier<T, T>, allows ref struct
Parameters
Type Name Description
T location

Reference to a value to be modified.

TUpdater updater

A side-effect-free function.

Returns
Type Description
T

The updated value.

Type Parameters
Name Description
T
TUpdater

The type implementing updater.

View Source

Write(ref double, double)

Writes atomically the value at the specified location in the memory.

Declaration
[CLSCompliant(false)]
[MemoryOrdering(MemoryOrdering.Release)]
public static void Write(ref double location, double value)
Parameters
Type Name Description
double location

The location of the value.

double value

The desired value at the specified location.

Remarks

This method works correctly on 32-bit and 64-bit architectures.

View Source

Write(ref long, long)

Writes atomically the value at the specified location in the memory.

Declaration
[MemoryOrdering(MemoryOrdering.Release)]
public static void Write(ref long location, long value)
Parameters
Type Name Description
long location

The location of the value.

long value

The desired value at the specified location.

Remarks

This method works correctly on 32-bit and 64-bit architectures.

View Source

Write(ref ulong, ulong)

Writes atomically the value at the specified location in the memory.

Declaration
[CLSCompliant(false)]
[MemoryOrdering(MemoryOrdering.Release)]
public static void Write(ref ulong location, ulong value)
Parameters
Type Name Description
ulong location

The location of the value.

ulong value

The desired value at the specified location.

Remarks

This method works correctly on 32-bit and 64-bit architectures.

View Source

Write<T>(T**, T*)

Writes the pointer to the specified location. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: if a read or write appears before this method in the code, the processor cannot move it after this method.

Declaration
[CLSCompliant(false)]
[MemoryOrdering(MemoryOrdering.Release)]
public static void Write<T>(T** location, T* value) where T : unmanaged
Parameters
Type Name Description
T** location

The location of the pointer to write.

T* value

The pointer to write.

Type Parameters
Name Description
T

The type of the value referenced by the pointer.

Remarks

Unlike volatile field modifier, this method can be used for the location obtained through the pointer.

View Source

Write<T>(ref T*, T*)

Writes the pointer to the specified location. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: if a read or write appears before this method in the code, the processor cannot move it after this method.

Declaration
[CLSCompliant(false)]
[MemoryOrdering(MemoryOrdering.Release)]
public static void Write<T>(ref T* location, T* value) where T : unmanaged
Parameters
Type Name Description
T* location

The location of the pointer to write.

T* value

The pointer to write.

Type Parameters
Name Description
T

The type of the value referenced by the pointer.

Remarks

Unlike volatile field modifier, this method can be used for any location passed by reference, such as an array element or a field of the struct accessed by reference.

View Source

get_IsUndefined<T>(ref readonly Atomic<Optional<T>>)

Indicates that the value withing the receiver is not set.

Declaration
[MemoryOrdering(MemoryOrdering.Acquire)]
public static bool get_IsUndefined<T>(ref readonly Atomic<Optional<T>> atomic)
Parameters
Type Name Description
Atomic<Optional<T>> atomic
Returns
Type Description
bool
Type Parameters
Name Description
T
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾