Questions tagged [atomic]

An atomic operation is indivisible. This term is used to describe transactions in databases, low-level accesses in multithreaded programs, and file system operations, amongst others.

An operation is atomic if it is indivisible --- either all the effects of the operation are visible, or none are.

In databases, the atomicity of transactions is one of the basic guarantees --- it is the A in ACID. This allows you to ensure that the data changes from one consistent state to another, even when updates cover more than one table.


In multithreaded programs, atomicity is important for building low-level facilities, as it ensures that code running on other threads either see the value before a change or the value after, and not some intermediate value. Locks are implemented terms of atomic operations, but they can also be used directly to create algorithms.

Atomicity is a property of a single memory operation (a store, a load, or a read-modify-write). In assembly language, aligned loads and stores are usually atomic by default (like on x86), but a read-modify-write like num++ isn't.

Transactional memory systems allow changes to multiple variables to be done as a single atomic operation, akin to database transactions.

In high-level languages, where the compiler takes care of keeping variables in registers or memory, it's not always safe to assume anything about when/if stores/loads actually happen. Some languages provide types where all operations are atomic (for example C11's and C++11's ).

3772 questions
105
votes
3 answers

What do each memory_order mean?

I read a chapter and I didn't like it much. I'm still unclear what the differences is between each memory order. This is my current speculation which I understood after reading the much more simple…
user34537
103
votes
5 answers

Understanding std::atomic::compare_exchange_weak() in C++11

bool compare_exchange_weak (T& expected, T val, ..); compare_exchange_weak() is one of compare-exchange primitives provided in C++11. It's weak in the sense that it returns false even if the value of the object is equal to expected. This is due to…
Eric Z
  • 14,327
  • 7
  • 45
  • 69
90
votes
5 answers

c++, std::atomic, what is std::memory_order and how to use them?

Can anyone explain what is std::memory_order in plain English, and how to use them with std::atomic<>? I found the reference and few examples here, but don't understand at all. http://en.cppreference.com/w/cpp/atomic/memory_order
2607
  • 4,037
  • 13
  • 49
  • 64
87
votes
3 answers

What operations in Java are considered atomic?

What operations in Java are considered atomic?
robinmag
  • 17,520
  • 19
  • 54
  • 55
85
votes
3 answers

Where is the lock for a std::atomic?

If a data structure has multiple elements in it, the atomic version of it cannot (always) be lock-free. I was told that this is true for larger types because the CPU can not atomically change the data without using some sort of lock. for…
curiousguy12
  • 1,741
  • 1
  • 10
  • 15
85
votes
10 answers

Django: How can I protect against concurrent modification of database entries

If there a way to protect against concurrent modifications of the same data base entry by two or more users? It would be acceptable to show an error message to the user performing the second commit/save operation, but data should not be silently…
Ber
  • 40,356
  • 16
  • 72
  • 88
85
votes
2 answers

Must I call atomic load/store explicitly?

C++11 introduced the std::atomic<> template library. The standard specifies the store() and load() operations to atomically set / get a variable shared by more than one thread. My question is are assignment and access operations also atomic?…
bavaza
  • 10,319
  • 10
  • 64
  • 103
84
votes
1 answer

What are atomic operations for newbies?

I am a newbie to operating systems and every answer I've found on Stackoverflow is so complicated that I am unable to understand. Can someone provide an explanation for what is an atomic operation For a newbie? My understanding: My understanding…
76
votes
7 answers

How to make file creation an atomic operation?

I am using Python to write chunks of text to files in a single operation: open(file, 'w').write(text) If the script is interrupted so a file write does not complete I want to have no file rather than a partially complete file. Can this be done?
hoju
  • 28,392
  • 37
  • 134
  • 178
74
votes
3 answers

Why is the volatile qualifier used through out std::atomic?

From what I've read from Herb Sutter and others you would think that volatile and concurrent programming were completely orthogonal concepts, at least as far as C/C++ are concerned. However, in GCC implementation all of std::atomic's member…
deft_code
  • 57,255
  • 29
  • 141
  • 224
73
votes
2 answers

Is id = 1 - id atomic?

From page 291 of OCP Java SE 6 Programmer Practice Exams, question 25: public class Stone implements Runnable { static int id = 1; public void run() { id = 1 - id; if (id == 0) pick(); else …
Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
72
votes
6 answers

How to implement multithread safe singleton in C++11 without using

Now that C++11 has multithreading I was wondering what is the correct way to implement lazy initialized singleton without using mutexes(for perf reasons). I came up with this, but tbh Im not really good at writing lockfree code, so Im looking for…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
69
votes
4 answers

How do "acquire" and "consume" memory orders differ, and when is "consume" preferable?

The C++11 standard defines a memory model (1.7, 1.10) which contains memory orderings, which are, roughly, "sequentially-consistent", "acquire", "consume", "release", and "relaxed". Equally roughly, a program is correct only if it is race-free,…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
68
votes
9 answers

Java: is there no AtomicFloat or AtomicDouble?

I have found AtomicInteger, AtomicLong, but where is AtomicFloat (or AtomicDouble)? Maybe there is some trick?
itun
  • 3,439
  • 12
  • 51
  • 75
67
votes
4 answers

How are atomic operations implemented at a hardware level?

I get that at the assembly language level instruction set architectures provide compare and swap and similar operations. However, I don't understand how the chip is able to provide these guarantees. As I imagine it, the execution of the instruction…
Alexander Duchene
  • 1,107
  • 1
  • 11
  • 14