System.Threading.Interlocked Class

public sealed class Interlocked

Base Types

Object
  Interlocked

Assembly

mscorlib

Library

BCL

Summary

The Interlocked class provides atomic operations for variables that are shared by multiple threads.

Description

The Interlocked methods protect against errors that can occur when the scheduler switches contexts while a thread is updating a variable that can be accessed by other threads. The members of this class do not throw exceptions.

[Note: The System.Threading.Interlocked.Increment(System.Int32@) method and its counterpart, System.Threading.Interlocked.Decrement(System.Int32@), increment or decrement a variable and store the resulting value, as an atomic operation.

The System.Threading.Interlocked.Exchange(System.Int32@,System.Int32) method atomically exchanges the values of the specified variables. The System.Threading.Interlocked.CompareExchange(System.Int32@,System.Int32,System.Int32) method provides an atomic operation that compares two values and stores a third value in one of the variables, based on the outcome of the comparison.

]

See Also

System.Threading Namespace

Members

Interlocked Methods

Interlocked.CompareExchange(System.Int32&, int, int) Method
Interlocked.CompareExchange(System.Single&, float, float) Method
Interlocked.CompareExchange(System.Object&, System.Object, System.Object) Method
Interlocked.Decrement(System.Int32&) Method
Interlocked.Decrement(System.Int64&) Method
Interlocked.Exchange(System.Int32&, int) Method
Interlocked.Exchange(System.Single&, float) Method
Interlocked.Exchange(System.Object&, System.Object) Method
Interlocked.Increment(System.Int32&) Method
Interlocked.Increment(System.Int64&) Method


Interlocked.CompareExchange(System.Int32&, int, int) Method

public static int CompareExchange(ref int location1, int value, int comparand);

Summary

Compares two Int32 values for equality and stores a specified value if they are equal.

Parameters

location1
A Int32 reference whose value is updated with value if the original value of location1 is equal to comparand.
value
A Int32 whose value will replace the value of location1 if location1 and comparand are equal.
comparand
A Int32 to be compared to location1.

Return Value

The original value of location1.

Description

The compare and store operations are performed as an atomic operation.

See Also

System.Threading.Interlocked Class, System.Threading Namespace

Interlocked.CompareExchange(System.Single&, float, float) Method

public static float CompareExchange(ref float location1, float value, float comparand);

Summary

Compares two Single values for equality and stores a specified value if they are equal.

Parameters

location1
A Single whose value is updated with value if its original value is equal to comparand.
value
The Single value that will replace value of location1 if location1 and comparand are equal.
comparand
A Single to be compared to location1.

Return Value

A Single containing the original value of location1.

Exceptions

Exception TypeCondition
ArgumentNullExceptionThe address of location1 is null .

Description

The compare and store operations are performed as an atomic operation.

Library

ExtendedNumerics

See Also

System.Threading.Interlocked Class, System.Threading Namespace

Interlocked.CompareExchange(System.Object&, System.Object, System.Object) Method

public static object CompareExchange(ref object location1, object value, object comparand);

Summary

Compares two Object variables for equality and stores a specified object if they are equal.

Parameters

location1
A Object reference that is set to value if the object to which it refers is equal to comparand.
value
The reference that will replace the value of location1 if location1 and comparand are equal.
comparand
An object to be compared to that referred to by location1.

Return Value

A Object containing the original value of location1.

Exceptions

Exception TypeCondition
ArgumentNullExceptionThe address of location1 is null .

Description

The compare and store operations are performed as an atomic operation.

See Also

System.Threading.Interlocked Class, System.Threading Namespace

Interlocked.Decrement(System.Int32&) Method

public static int Decrement(ref int location);

Summary

Decrements the specified variable and stores the result as an atomic operation.

Parameters

location
A Int32 containing the variable whose value is to be decremented.

Return Value

A Int32 containing the decremented value.

Description

This method handles an overflow condition by wrapping: if location = System.Int32.MinValue , location - 1 = System.Int32.MaxValue . No exception is thrown.

See Also

System.Threading.Interlocked Class, System.Threading Namespace

Interlocked.Decrement(System.Int64&) Method

public static long Decrement(ref long location);

Summary

Decrements the specified variable and stores the result as an atomic operation.

Parameters

location
A Int64 containing the variable whose value is to be decremented.

Return Value

A Int64 containing the decremented value.

Description

This method handles an overflow condition by wrapping: if location = System.Int64.MinValue , location - 1 = System.Int64.MaxValue . No exception is thrown.

The 64-bit versions of System.Threading.Interlocked.Increment(System.Int32@) and System.Threading.Interlocked.Decrement(System.Int32@) are truly atomic only on systems where a IntPtr is 64-bits long. On other systems, these methods are atomic with respect to each other, but not with respect to other means of accessing the data.

See Also

System.Threading.Interlocked Class, System.Threading Namespace

Interlocked.Exchange(System.Int32&, int) Method

public static int Exchange(ref int location1, int value);

Summary

Sets a Int32 variable to a specified value as an atomic operation and returns the original value.

Parameters

location1
A Int32 variable to set to the supplied value as an atomic operation.
value
The Int32 value to which location1 is set.

Return Value

A Int32 containing the value of location1 before the exchange.

See Also

System.Threading.Interlocked Class, System.Threading Namespace

Interlocked.Exchange(System.Single&, float) Method

public static float Exchange(ref float location1, float value);

Summary

Sets a Single variable to a specified value as an atomic operation and returns the original value.

Parameters

location1
A Single variable to set to the supplied value as an atomic operation.
value
The Single value to which location1 is set.

Return Value

A Single containing the value of location1 before the exchange.

Library

ExtendedNumerics

See Also

System.Threading.Interlocked Class, System.Threading Namespace

Interlocked.Exchange(System.Object&, System.Object) Method

public static object Exchange(ref object location1, object value);

Summary

Sets a Object reference to refer to a specified object as an atomic operation and returns a reference to the original object.

Parameters

location1
The variable to set.
value
The reference to which location1 is set.

Return Value

The original value of location1 .

Exceptions

Exception TypeCondition
ArgumentNullExceptionThe address of location1 is null .

See Also

System.Threading.Interlocked Class, System.Threading Namespace

Interlocked.Increment(System.Int32&) Method

public static int Increment(ref int location);

Summary

Increments the specified variable and stores the result as an atomic operation.

Parameters

location
A Int32 containing the variable whose value is to be incremented.

Return Value

A Int32 containing the incremented value.

Description

This method handles an overflow condition by wrapping: if location = System.Int32.MaxValue , location + 1 = System.Int32.MinValue . No exception is thrown.

See Also

System.Threading.Interlocked Class, System.Threading Namespace

Interlocked.Increment(System.Int64&) Method

public static long Increment(ref long location);

Summary

Increments the specified variable and stores the result as an atomic operation.

Parameters

location
A Int64 containing the variable whose value is to be incremented.

Return Value

A Int64 containing the incremented value.

Description

This method handles an overflow condition by wrapping: if location = System.Int64.MaxValue , location + 1 = System.Int64.MinValue . No exception is thrown.

The 64-bit versions of System.Threading.Interlocked.Increment(System.Int32@) and System.Threading.Interlocked.Decrement(System.Int32@) are truly atomic only on systems where a IntPtr is 64-bits long. On other systems, these methods are atomic with respect to each other, but not with respect to other means of accessing the data.

See Also

System.Threading.Interlocked Class, System.Threading Namespace