Show / Hide Table of Contents

Struct Atomic<T>

Provides atomic access to non-primitive data type.

Implements
IStrongBox
ICloneable
Inherited Members
ValueType.Equals(object)
ValueType.GetHashCode()
object.Equals(object, object)
object.GetType()
object.ReferenceEquals(object, object)
Namespace: DotNext.Threading
Assembly: DotNext.dll
Syntax
public struct Atomic<T> : IStrongBox, ICloneable where T : struct
Type Parameters
Name Description
T

The type of the value to be accessible in atomic manner.

Remarks

Synchronized methods can be declared in classes only. If you don't need to have extra heap allocation to keep synchronization root in the form of the object, or you need to have volatile field inside of value type then Atomic<T> is the best choice. Its performance is better than synchronized methods according to benchmarks.

Properties

| Edit this page View Source

Value

Gets or sets value atomically.

Declaration
public T Value { get; set; }
Property Value
Type Description
T
Remarks

To achieve best performance it is recommended to use Read(out T) and Write(in T) methods because they don't cause extra allocation of stack memory for passing value.

Methods

| Edit this page View Source

AccumulateAndGet(in T, Accumulator, out T)

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

Declaration
public void AccumulateAndGet(in T x, Atomic<T>.Accumulator accumulator, out T result)
Parameters
Type Name Description
T x

Accumulator operand.

Atomic<T>.Accumulator accumulator

A side-effect-free function of two arguments.

T result

The updated value.

Remarks

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

Exceptions
Type Condition
ArgumentNullException

accumulator is null.

| Edit this page View Source

Clone()

Clones this container atomically.

Declaration
public Atomic<T> Clone()
Returns
Type Description
Atomic<T>

The cloned container.

| Edit this page View Source

CompareAndSet(delegate*<in T, in T, bool>, in T, in T)

Atomically sets the stored value to the given updated value if the current value == the expected value.

Declaration
[CLSCompliant(false)]
public bool CompareAndSet(delegate*<in T, in T, bool> comparer, in T expected, in T update)
Parameters
Type Name Description
delegate*<in T, in T, bool> comparer

The function representing comparison logic.

T expected

The expected value.

T update

The new value.

Returns
Type Description
bool

true if successful. false return indicates that the actual value was not equal to the expected value.

| Edit this page View Source

CompareAndSet(Func<T, T, bool>, in T, in T)

Atomically sets the stored value to the given updated value if the current value == the expected value.

Declaration
public bool CompareAndSet(Func<T, T, bool> comparer, in T expected, in T update)
Parameters
Type Name Description
Func<T, T, bool> comparer

The function representing comparison logic.

T expected

The expected value.

T update

The new value.

Returns
Type Description
bool

true if successful. false return indicates that the actual value was not equal to the expected value.

| Edit this page View Source

CompareAndSet(in T, in T)

Atomically sets the stored value to the given updated value if the current value == the expected value.

Declaration
public bool CompareAndSet(in T expected, in T update)
Parameters
Type Name Description
T expected

The expected value.

T update

The new value.

Returns
Type Description
bool

true if successful. false return indicates that the actual value was not equal to the expected value.

| Edit this page View Source

CompareExchange(delegate*<in T, in T, bool>, in T, in T, out T)

Compares two values of type T for equality and, if they are equal, replaces the stored value.

Declaration
[CLSCompliant(false)]
public bool CompareExchange(delegate*<in T, in T, bool> comparer, in T update, in T expected, out T result)
Parameters
Type Name Description
delegate*<in T, in T, bool> comparer

The function representing comparison logic.

T update

The value that replaces the stored value if the comparison results in equality.

T expected

The value that is compared to the stored value.

T result

The origin value stored in this container before modification.

Returns
Type Description
bool

true if the current value is replaced by update; otherwise, false.

| Edit this page View Source

CompareExchange(Func<T, T, bool>, in T, in T, out T)

Compares two values of type T for equality and, if they are equal, replaces the stored value.

Declaration
public bool CompareExchange(Func<T, T, bool> comparer, in T update, in T expected, out T result)
Parameters
Type Name Description
Func<T, T, bool> comparer

The function representing comparison logic.

T update

The value that replaces the stored value if the comparison results in equality.

T expected

The value that is compared to the stored value.

T result

The origin value stored in this container before modification.

Returns
Type Description
bool

true if the current value is replaced by update; otherwise, false.

| Edit this page View Source

CompareExchange(in T, in T, out T)

Compares two values of type T for bitwise equality and, if they are equal, replaces the stored value.

Declaration
public bool CompareExchange(in T update, in T expected, out T result)
Parameters
Type Name Description
T update

The value that replaces the stored value if the comparison results in equality.

T expected

The value that is compared to the stored value.

T result

The origin value stored in this container before modification.

Returns
Type Description
bool

true if the current value is replaced by update; otherwise, false.

| Edit this page View Source

Exchange(in T, out T)

Sets a value stored in this container to a specified value and returns the original value, as an atomic operation.

Declaration
public void Exchange(in T update, out T previous)
Parameters
Type Name Description
T update

The value that replaces the stored value.

T previous

The original stored value before modification.

| Edit this page View Source

GetAndAccumulate(in T, Accumulator, out T)

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

Declaration
public void GetAndAccumulate(in T x, Atomic<T>.Accumulator accumulator, out T result)
Parameters
Type Name Description
T x

Accumulator operand.

Atomic<T>.Accumulator accumulator

A side-effect-free function of two arguments.

T result

The original value.

Remarks

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

Exceptions
Type Condition
ArgumentNullException

accumulator is null.

| Edit this page View Source

GetAndUpdate(Updater, out T)

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

Declaration
public void GetAndUpdate(Atomic<T>.Updater updater, out T result)
Parameters
Type Name Description
Atomic<T>.Updater updater

A side-effect-free function.

T result

The original value.

Exceptions
Type Condition
ArgumentNullException

updater is null.

| Edit this page View Source

Read(out T)

Performs atomic read.

Declaration
public void Read(out T result)
Parameters
Type Name Description
T result

The result of atomic read.

| Edit this page View Source

Swap(ref Atomic<T>)

Swaps the value stored in this container and the given value atomically.

Declaration
public void Swap(ref Atomic<T> other)
Parameters
Type Name Description
Atomic<T> other

The container for the value.

Remarks

This operation is atomic for both containers.

| Edit this page View Source

Swap(ref T)

Swaps the value stored in this container and the given value atomically.

Declaration
public void Swap(ref T other)
Parameters
Type Name Description
T other

The managed pointer to the value to swap.

| Edit this page View Source

ToString()

Converts the stored value into string atomically.

Declaration
public override string? ToString()
Returns
Type Description
string

The string returned from ToString() method called on the stored value.

Overrides
ValueType.ToString()
| Edit this page View Source

UpdateAndGet(Updater, out T)

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

Declaration
public void UpdateAndGet(Atomic<T>.Updater updater, out T result)
Parameters
Type Name Description
Atomic<T>.Updater updater

A side-effect-free function.

T result

The updated value.

Exceptions
Type Condition
ArgumentNullException

updater is null.

| Edit this page View Source

Write(in T)

Performs atomic write.

Declaration
public void Write(in T newValue)
Parameters
Type Name Description
T newValue

The value to be stored into this container.

Implements

IStrongBox
ICloneable

Extension Methods

BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, ReadOnlySpan<T>)
Collection.ToAsyncEnumerator<TEnumerator, T>(TEnumerator, CancellationToken)
Collection.ToClassicEnumerator<TEnumerator, T>(TEnumerator)
Enumerator.Skip<TEnumerator, T>(ref TEnumerator, int)
ExpressionBuilder.Const<T>(T)
  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾