Interlocked.Exchange Method
.NET Framework 4
Sets a variable to a specified value as an atomic operation.
This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.
| Name | Description | |
|---|---|---|
|
Exchange<T>(T, T) | Sets a variable of the specified type T to a specified value and returns the original value, as an atomic operation. |
|
Exchange(Double, Double) | Sets a double-precision floating point number to a specified value and returns the original value, as an atomic operation. |
|
Exchange(Int32, Int32) | Sets a 32-bit signed integer to a specified value and returns the original value, as an atomic operation. |
|
Exchange(Int64, Int64) | Sets a 64-bit signed integer to a specified value and returns the original value, as an atomic operation. |
|
Exchange(IntPtr, IntPtr) | Sets a platform-specific handle or pointer to a specified value and returns the original value, as an atomic operation. |
|
Exchange(Object, Object) | Sets an object to a specified value and returns a reference to the original object, as an atomic operation. |
|
Exchange(Single, Single) | Sets a single-precision floating point number to a specified value and returns the original value, as an atomic operation. |
If somebody wants boolean support...
You can define a class which wraps a boolean type in it and use a generic variant of Exchange Method.
However, it would be better to use int32 version because there is a machine code for interlocked exchanged for that.
However, it would be better to use int32 version because there is a machine code for interlocked exchanged for that.
- 5/31/2012
- JongAm Park
Why bool is not there.
Atomic operations involve a single processor instruction. A bool would be inefficient because for a processor to get to a bool it needs to potentially rotate a processor variable left or right to get at the "bit" that contains the bool in the 32bit/64bit processor to bit test it. This would be a mutiple instruction and hence the whole idea of an atomic single instruction lock/swap goes out the window. ie It would depend on how the CLR held a bool,, in a 32bit processor variable there are potentially 32 bools etc.
The alternative would be that the bool is in fact an int32/int64 in which would allow an atomic swap and be very processor efficient because there would be no rotating left/right to bit test. Another alternative would be to "and" the 32 bits with a bit test of the bool concerned (ie 1111 & 1000 would test the 4th bit) but again at least 2+ instructions so that wont work either. Makes the whole point of trying to space save using a bool a bit pointless.
The alternative would be that the bool is in fact an int32/int64 in which would allow an atomic swap and be very processor efficient because there would be no rotating left/right to bit test. Another alternative would be to "and" the 32 bits with a bit test of the bool concerned (ie 1111 & 1000 would test the 4th bit) but again at least 2+ instructions so that wont work either. Makes the whole point of trying to space save using a bool a bit pointless.
- 1/13/2012
- MaxDaniels