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
1888
votes
26 answers

What's the difference between the atomic and nonatomic attributes?

What do atomic and nonatomic mean in property declarations? @property(nonatomic, retain) UITextField *userName; @property(atomic, retain) UITextField *userName; @property(retain) UITextField *userName; What is the operational difference between…
Alex Wayne
  • 145,435
  • 42
  • 271
  • 302
320
votes
7 answers

What is the difference between atomic / volatile / synchronized?

How do atomic / volatile / synchronized work internally? What is the difference between the following code blocks? Code 1 private int counter; public int getNextUniqueIndex() { return counter++; } Code 2 private AtomicInteger counter; public…
hardik
  • 7,975
  • 7
  • 26
  • 48
309
votes
6 answers

What does "atomic" mean in programming?

In the Effective Java book, it states: The language specification guarantees that reading or writing a variable is atomic unless the variable is of type long or double [JLS, 17.4.7]. What does "atomic" mean in the context of Java programming,…
James
  • 3,093
  • 3
  • 11
  • 4
248
votes
12 answers

Practical uses for AtomicInteger

I sort of understand that AtomicInteger and other Atomic variables allow concurrent accesses. In what cases is this class typically used though?
James P.
  • 17,929
  • 27
  • 89
  • 147
206
votes
3 answers

What exactly is std::atomic?

I understand that std::atomic<> is an atomic object. But atomic to what extent? To my understanding an operation can be atomic. What exactly is meant by making an object atomic? For example if there are two threads concurrently executing the…
user4386938
161
votes
13 answers

Can num++ be atomic for 'int num'?

In general, for int num, num++ (or ++num), as a read-modify-write operation, is not atomic. But I often see compilers, for example GCC, generate the following code for it (try here): void f() { int num = 0; num++; } f(): push rbp …
Lmn
  • 3,527
  • 3
  • 12
  • 27
145
votes
5 answers

When to use volatile with multi threading?

If there are two threads accessing a global variable then many tutorials say make the variable volatile to prevent the compiler caching the variable in a register and it thus not getting updated correctly. However two threads both accessing a shared…
David Preston
  • 1,771
  • 2
  • 11
  • 10
140
votes
8 answers

Is the != check thread safe?

I know that compound operations such as i++ are not thread safe as they involve multiple operations. But is checking the reference with itself a thread safe operation? a != a //is this thread-safe I tried to program this and use multiple threads…
Narendra Pathai
  • 38,384
  • 18
  • 73
  • 117
120
votes
6 answers

AtomicInteger lazySet vs. set

What is the difference between the lazySet and set methods of AtomicInteger? The documentation doesn't have much to say about lazySet: Eventually sets to the given value. It seems that the stored value will not be immediately set to the desired…
Cheok Yan Cheng
  • 49,649
  • 117
  • 410
  • 768
114
votes
7 answers

What is the difference between atomic and critical in OpenMP?

What is the difference between atomic and critical in OpenMP? I can do this #pragma omp atomic g_qCount++; but isn't this same as #pragma omp critical g_qCount++; ?
codereviewanskquestions
  • 11,772
  • 25
  • 93
  • 158
112
votes
4 answers

Is file append atomic in UNIX?

In general, what can we take for granted when we append to a file in UNIX from multiple processes? Is it possible to lose data (one process overwriting the other's changes)? Is it possible for data to get mangled? (For example, each process is…
Lajos Nagy
  • 7,832
  • 11
  • 41
  • 52
94
votes
3 answers

atomic operation cost

What is the cost of the atomic operation (any of compare-and-swap or atomic add/decrement)? How much cycles does it consume? Will it pause other processors on SMP or NUMA, or will it block memory accesses? Will it flush reorder buffer in…
osgx
  • 80,853
  • 42
  • 303
  • 470
93
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
  • 13,619
  • 5
  • 40
  • 65
84
votes
7 answers

Which is more efficient, basic mutex lock or atomic integer?

For something simple like a counter if multiple threads will be increasing the number. I read that mutex locks can decrease efficiency since the threads have to wait. So, to me, an atomic counter would be the most efficient, but I read that…
Matt
  • 849
  • 1
  • 7
  • 3
82
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
  • 34,859
  • 15
  • 60
  • 79
1
2 3
99 100