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 with benchmarks.

Properties

| Improve this Doc 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(T) methods because they don't cause extra allocation of stack memory for passing value.

Methods

| Improve this Doc View Source

AccumulateAndGet(T, Atomic<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.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.

| Improve this Doc View Source

Clone()

Clones this container atomically.

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

The cloned container.

| Improve this Doc View Source

CompareAndSet(T, 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
Boolean

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

| Improve this Doc View Source

CompareExchange(T, 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
Boolean

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

| Improve this Doc View Source

Exchange(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.

| Improve this Doc View Source

GetAndAccumulate(T, Atomic<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.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.

| Improve this Doc View Source

GetAndUpdate(Atomic<T>.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.Updater<> updater

A side-effect-free function.

T result

The original value.

Exceptions
Type Condition
ArgumentNullException

updater is null.

| Improve this Doc 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.

| Improve this Doc 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.

| Improve this Doc 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.

| Improve this Doc 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()
| Improve this Doc View Source

UpdateAndGet(Atomic<T>.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.Updater<> updater

A side-effect-free function.

T result

The updated value.

Exceptions
Type Condition
ArgumentNullException

updater is null.

| Improve this Doc View Source

Write(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.

Explicit Interface Implementations

| Improve this Doc View Source

ICloneable.Clone()

Declaration
object ICloneable.Clone()
Returns
Type Description
Object
| Improve this Doc View Source

IStrongBox.Value

Declaration
object IStrongBox.Value { get; set; }
Returns
Type Description
Object

Implements

System.Runtime.CompilerServices.IStrongBox
System.ICloneable

Extension Methods

Sequence.Skip<TEnumerator, T>(ref TEnumerator, Int32)
ExpressionBuilder.Const<T>(T)
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX