I'm really struggling to find a concrete, easy to grasp, explanation of Atomicity.

My understanding thus far is that to ensure an operation is atomic you wrap the critical code in a locker. But that's about as much as I actually understand. Definitions such as the one below make no sense to me at all.

An operation during which a processor can simultaneously read a location and write it in the same bus operation. This prevents any other processor or I/O device from writing or reading memory until the operation is complete. Atomic implies indivisibility and irreducibility, so an atomic operation must be performed entirely or not performed at all.

What does the last sentence mean? Is the term indivisibility relating to mathematics or something else?

Sometimes the jargon with these topics confuse more than they teach.

  • 2
    Indivisibility: You can make an atomic operation that consist of multiple statements. These statements could be executed on their own, but in your system, the atomic operation itself can only be executed in its entirety or not at all. So you cannot execute your atomic operation separated into parts - it will always execute as one statement. May 24, 2014 at 12:16
  • @owlstead care to post as an answer with an example? Pseudo code or any language of choice. May 24, 2014 at 12:26
  • 2
    Did you read en.wikipedia.org/wiki/Atomicity_(programming) ?
    – Euphoric
    May 24, 2014 at 12:39
  • @Euphoric yes I read that. I was trying to find an MSDN article with some code examples. May 24, 2014 at 12:43
  • @JamesJeffery Well. MSDN is primarily MS-centric. Atomicity is much more general term. It is hard to say what you are asking if you are mixing such general term as atomicity with specific language like C#.
    – Euphoric
    May 24, 2014 at 12:46

3 Answers 3


Atomicity is a trait that defines wether an operation can be interrupted or not. In general, it does not refer to a bunch of operations in a lock. A lock doesn't guarantee that nothing else can run during the lock, just nothing else can run that code, on that instance during the lock.

Let's look at an example: int x = 4 * sin(pi)

Without optimization, this statement is not atomic.

  • Fetching pi will usually be atomic, assuming it is a double and your processor can read a double in one clock cycle.
  • Calling sin and returning its result (assuming sin is some function and not something like a macro) is not atomic since something can run between the code running and returning the value.
    • Granted, this probably doesn't matter. pi will be copied going into the function and sin is unlikely to be a mutable function. So while this is threadsafe (in common usage) it is not atomic.
  • On most all processors multiplying two doubles and assigning the result will be an atomic operation since it will be done in one operation by the processor.

So, key things to note:

  • Having two atomic operations does not yield another one, since the processor can switch to another thread in between them.
  • An operation that is atomic on one machine may not be on another.
  • Optimization can make an operation that isn't atomic into one that is (and occasionally vice versa!)
  • Atomicity matters because if something can't be interrupted, it's intrinsically thread safe. Locks are expensive, so atomic operations in key spots can vastly improve concurrent performance and correctness.
  • 1
    This is an excellent answer that helps me visualise Atomicity. Could you include an example of an atomic operation? May 24, 2014 at 14:36
  • 1
    @jamesjeffery - int x = 4 * 3.14 would be atomic on almost every platform since add/mul with assignment is often done in one operation (and literals/constants are "free"). Compare with int x = y * z; this isn't atomic if y and z are variables since "get y” and "get z" can be interrupted (by "change y" or "change z").
    – Telastyn
    May 24, 2014 at 15:05

Personally I wouldn't call an atomic operation uninteruptible, after all you could blow the machine up. :) I'd say it was one that could not be, through all sorts of mechanisms of varying effectiveness and cost, partially complete.

It either all happened (no matter how big or small is) or it didn't happen at all.

  • Dear downvoter, you'll get it one day, it's called abstraction May 28, 2014 at 9:24

As an example, you go to the bathroom, lock the door, do your toilet, wash and dry your hands and then unlock the door.

Absent an emergency (such as a fire alarm) you expect to complete the whole process uninterrupted. It is as single "black box" event from the perspective of some one on the outside who sees A) you go in; B) you come out.

(The equivalent of a fire alarm would be a higher level interrupt).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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