Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

From the discussion I've seen it seems that atomic operation and thread safety are the same thing, but a lot of people say that they're different. Can anyone tell me the difference if there is one?

share|improve this question
3  
Atomic operations will help ensuring thread safety, but how could they possibly be the same thing? A "thread" is not the same as a "operation". –  user50849 Dec 10 '12 at 10:09

4 Answers 4

up vote 11 down vote accepted

Atomic operations are a way to achive thread safety either by using some kind of locks like Mutexes or Semaphores which use atomic operations internally or by implementing lock free synchronization using atomics and memory fences.

So atomic operations on primitive data types are a tool to achive thread safety but do not ensure thread safety automatically because you normally have multiple operations that rely on each other. You have to ensure that these operations are done without interruption eg using Mutexes.

Yes, writing one of these atomic data types in c# is thread safe, but that does not make the function you use them in thread safe. It only ensures that the single write is correctly executed even if a second thread accesses it "at the same time". Never the less, the next read from the current thread is not ensured to get the value previously written as a different thread might have written to it, only that the value read is valid.

share|improve this answer
    
Is int, bool, float are thread safe or atomic? –  user960567 Dec 10 '12 at 10:27
1  
@user960567 - data types are just that: data types. It is up to the compiler to decide how to access them. Think of an int64 on a 8086 CPU. –  mouviciel Dec 10 '12 at 10:30
2  
In C# specifically, that the question is tagged with, reads and writes must be atomic on the basic data-types. See Ecma 334 –  user50849 Dec 10 '12 at 10:39
2  
And yes, writing one of these atomic data types in c# is thread safe, but that does not make the function you use them in thread safe. It only ensures that the single write is correctly executed even if a second thread accesses it "at the same time". Never the less, the next read from the current thread is not ensured to get the value previously written as a different thread might have written to it, only that the value read is valid. –  Archy Dec 10 '12 at 10:47
3  
x=5 is atomic in c#. But immediately after this operation it could be overwritten. x=x+1 is executed by 1. loading x into register 2. increment x in register 3. store x into memory. If a second thread does the same thing at the same time, both will load the same value, increment it and store it resulting in x only getting incremented once instead of twice. InterlockedIncrement either uses a special processor instruction to execute a atomic increment or ensures this by using a locking mechanism, eg CAS, to ensure that while the new value is not written no other thread may read the old value. –  Archy Dec 10 '12 at 11:17

An atomic operation is an operation that cannot be interrupted.

A safe thread is a thread that can safely be interrupted.

Thread safety is obtained with atomic operations, in particular in the logic that prevents critical resources from being accessed multiple times.

The basic atomic operation is Test-and-set, which is used for implementing semaphores, which in turn are used to implement thread safety.

share|improve this answer
    
Cannot a multi-step operation be interrupted and be called atomic still if it is guaranteed to roll back it's changes? –  user50849 Dec 10 '12 at 10:21
1  
No. Atomic is to be understood in its etymological meaning: ἄτομος, atomos, indivisible. –  mouviciel Dec 10 '12 at 10:26
    
Is int, bool, float are thread safe or atomic? –  user960567 Dec 10 '12 at 10:28
    
But isn't there's a difference between being indivisble, and appearing indivisible to a observer? By your definition, not atomic operation could contain more than a single step. I believe the "appears" word in wikipedia's definition of Atomic operation is important. (I'm in the chat if anyone wants to bring it up there) :) –  user50849 Dec 10 '12 at 10:36
    
There is one big difference: a safe thread can be interrupted, and there is no garantee of how much time. This is critical in realtime computing. An atomic operation (with interrupt locks if it is multi-step) is garanteed to terminate after a predictable amount of time. –  mouviciel Dec 10 '12 at 10:44

Atomicity and thread-safety are two different things. Atomicity refers to an operation's "all-or-nothing" quality; if an operation cannot be performed 100% successfully, then the system should remain in the overall state it had been in before any part of the operation began. The classic example is a database transaction; When saving an invoice, including its header and multiple line items, every single part of every single database row must be put in place successfully; if not, data is lost or corrupted. If a line item cannot be inserted, then not only should no other rows remaining be inserted, but none of the rows already processed should remain.

Thread-safety refers to a combination of things, including atomicity, that allows an operation to be "reentrant"; multiple workers can be performing the same operation, starting at the same or different times, without effect on any other. There are many models for thread-safe operation; most of them boil down conceptually to either running multiple parallel tasks in complete isolation (two workers can perform the same task on two different objects or collections of objects without ever knowing the other worker even exists), or setting up a "pipeline" within which multiple workers each perform one task out of an entire operation (either each worker progresses from the first task to the next and so on, or else focuses on one task and hands off its intermediate "work product" to the next worker).

share|improve this answer

Thread-safety is more a framework or a "concept", atomic operation is a subset, a means (one of many) of achieving status as being classed 'thread-safe'.

Thread safety refers to a process that can be accessed by separate threads, where the access of one (and manipulation of data), wont corrupt the integrity of the operation of the other.

Much of the skill of the programmer is knowing how to achieve this, depending on the situation and key objective, you may need to implement, for example: locks, semaphores, latches, atomic objects, synchronization rules etc...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.