0

I've been hearing the term atomic variable for a while now and so far I don't no what is that, so I'd like to see an example and why we use atomic variable if possible in C# and thank you very much.

Boris B.
  • 4,933
  • 1
  • 28
  • 59
user2876339
  • 19
  • 1
  • 2
  • 2
    Do you mean "atomic read and write" aka ["atomic operations"](http://stackoverflow.com/questions/11745440/what-operations-are-atomic-in-c)? – Matthew Watson Oct 15 '13 at 09:27
  • 1
    [this](http://programmers.stackexchange.com/a/178903/30927) may help clarify things – Cristian Lupascu Oct 15 '13 at 09:28
  • 2
    There is no "atomic variable" in c# – Kamil Budziewski Oct 15 '13 at 09:28
  • In general term it's called "Atomicity", which refers to series of operations either all occures or nothing occurs. Please look at this how to do with .Net. http://msdn.microsoft.com/en-us/library/ms228964.aspx – mit Oct 15 '13 at 09:38
  • Also have a look at "Atomicity" at Wikipedia: http://en.wikipedia.org/wiki/Atomicity_%28programming%29 - section "Example atomic operation" explains it in a simple way. – gehho Oct 15 '13 at 09:42
  • i am confused. is primitive types like int char ( basically accumulator size of cpu) were supposed to be atomic . isnt it ? – adt Oct 15 '13 at 09:46
  • 1
    @adt: A type is not atomic, an operation is (or can be). The fact that a type fits into the accumulator or register does not make it atomic, since the variable accessed by the code is actually stored *in memory* (as in RAM). It is there where it's not thread safe, because of the registers. While the variable is loaded in a register there are two copies of the value (one in the register and one in the memory) so if there is a thread switch then the other thread would read from memory and obtain the old value, resulting in lost increments (example if both threads are incrementing the value). – Boris B. Oct 15 '13 at 10:09

1 Answers1

4

Atomic operations are thread-safe operations that execute atomically, that is there is no thread-switch while the operation is executing (or at least the result of a thread-switch is not observable from the outside) so practically the operation is executed as a one-step. On the .Net platform this is provided by the Interlocked class. Other platforms, such as Java provide various other classes, like AtomicInteger. An instance of the AtomicInteger (in Java) could be called an atomic variable, so I'm guessing that's what you are referring to when you say an atomic variable.

The main point about Atomic/Interlocked objects is that they don't require any outside locks or other synchronization objects to achieve atomicity and thus thread-safety.

Boris B.
  • 4,933
  • 1
  • 28
  • 59
  • 1
    Some operations are atomic without requiring the use of the Interlocked class. See http://msdn.microsoft.com/en-us/library/aa691278.aspx – Kris Vandermotten Oct 15 '13 at 10:22
  • The question was about "why we use atomic variables", I never said that there are no inherently atomic operations in C#. But since we are on the topic, the C# specs state that only read or write operations are atomic (and only for certain types, namely the ones that are up to 32 bits long), a read-modify-write is still not atomic. The Interlocked class provides atomic methods only for read-modify-write operations, plus an atomic read of a 64bit variable. – Boris B. Oct 15 '13 at 10:40
  • Sure, I never said that you said that there are no inherently atomic operations... Seriously, my comment was not criticism, I was only providing additional information to the readers. What triggered it was the phrase "On the .Net platform, [atomic operations are] provided by the Interlocked class." This is true, but might suggest to some readers that this class is the only way in which .Net provides atomic operations. That would be an incorrect interpretation, so I clarified it. – Kris Vandermotten Oct 15 '13 at 15:20