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 ).

2796 questions
74
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
  • 3,557
  • 12
  • 45
  • 62
74
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,241
  • 6
  • 12
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
  • 18,835
  • 4
  • 63
  • 104
72
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
  • 51,579
  • 27
  • 135
  • 215
71
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
  • 23,776
  • 22
  • 102
  • 222
64
votes
6 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
  • 24,959
  • 33
  • 122
  • 169
63
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
  • 428,875
  • 83
  • 813
  • 1,025
63
votes
2 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
60
votes
4 answers

Are C/C++ fundamental types atomic?

Are C/C++ fundamental types, like int, double, etc., atomic, e.g. threadsafe? Are they free from data races; that is, if one thread writes to an object of such a type while another thread reads from it, is the behavior well-defined? If not, does it…
carmellose
  • 4,146
  • 7
  • 35
  • 46
59
votes
2 answers

difference between standard's atomic bool and atomic flag

I wasn't aware of the std::atomic variables but was aware about the std::mutex (weird right!) provided by the standard; however one thing caught my eye: there are two seemingly-same (to me) atomic types provided by the standard, listed…
hg_git
  • 2,234
  • 5
  • 20
  • 42
59
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
  • 8,590
  • 10
  • 54
  • 88
58
votes
2 answers

writeToFile:atomically: what does atomically mean?

I am wondering what the atomically: parameter stands for in the writeToFile:atomically: method (-[NSArray writeToFile:atomically:] for example). It is common to pass YES for atomically:, but I don't know what it means.
JonasG
  • 9,035
  • 12
  • 53
  • 88
57
votes
8 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,121
  • 11
  • 47
  • 72
57
votes
5 answers

When is it preferable to use volatile boolean in Java rather than AtomicBoolean?

I've looked at the other volatile vs. Atomicxxxx questions in SO (including this one) and have read the description of java.util.current.atomic, and I am not quite satisfied with the nuances. If I'm trying to decide between using volatile boolean…
Jason S
  • 171,795
  • 155
  • 551
  • 900
56
votes
9 answers

How to perform atomic operations on Linux that work on x86, arm, GCC and icc?

Every Modern OS provides today some atomic operations: Windows has Interlocked* API FreeBSD has Solaris has Mac OS X has Anything like that for Linux? I need it to work on most Linux supported…
Artyom
  • 30,091
  • 20
  • 121
  • 208