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
42
votes
8 answers

How to atomically rename a file in Java, even if the dest file already exists?

I have a cluster of machines, each running a Java app. These Java apps need to access a unique resource.txt file concurrently. I need to atomically rename a temp.txt file to resource.txt in Java, even if resource.txt already exist. Deleting…
Sébastien RoccaSerra
  • 16,731
  • 8
  • 50
  • 54
41
votes
3 answers

Do I have to use atomic for "exit" bool variable?

I need to set a flag for another thread to exit. That other thread checks the exit flag from time to time. Do I have to use atomic for the flag or just a plain bool is enough and why (with an example of what exactly may go wrong if I use plain…
PowerGamer
  • 2,106
  • 2
  • 17
  • 33
40
votes
3 answers

Is x86 CMPXCHG atomic, if so why does it need LOCK?

The Intel documentation says This instruction can be used with a LOCK prefix to allow the instruction to be executed atomically. My question is Can CMPXCHG operate with memory address? From the document it seems not but can anyone confirm that…
Alex Suo
  • 2,977
  • 1
  • 14
  • 22
39
votes
2 answers

C++11 atomic memory ordering - is this a correct usage of relaxed (release-consume) ordering?

I have recently made a port to C++11 using std::atomic of a triple buffer to be used as a concurrency sync mechanism. The idea behind this thread sync approach is that for a producer-consumer situation where you have a producer that is running…
André Neves
  • 1,066
  • 10
  • 14
39
votes
3 answers

C++ atomic_flag query state

I am using C++ std::atomic_flag as an atomic Boolean flag. Setting the flag to true or false is not a problem but how to query the current state of flag without setting it to some value? I know that there are methods 'atomic_flag_clear' and…
polapts
  • 5,493
  • 10
  • 37
  • 49
38
votes
4 answers

Alignment requirements for atomic x86 instructions vs. MS's InterlockedCompareExchange documentation?

Microsoft offers the InterlockedCompareExchange function for performing atomic compare-and-swap operations. There is also an _InterlockedCompareExchange intrinsic. On x86 these are implemented using the lock cmpxchg instruction. However, reading…
jalf
  • 243,077
  • 51
  • 345
  • 550
37
votes
9 answers

Pop multiple values from Redis data structure atomically?

Is there a Redis data structure, which would allow atomic operation of popping (get+remove) multiple elements, which it contains? There are well known SPOP or RPOP, but they always return a single value. Therefore, when I need first N values from…
Pavel S.
  • 11,892
  • 18
  • 75
  • 113
37
votes
3 answers

C++11: write move constructor with atomic member?

I've got a class with an atomic member variable: struct Foo { std::atomic bar; /* ... lots of other stuff, not relevant here ... */ Foo() : bar( false ) {} /* Trivial implementation fails in gcc 4.7 with: * error: use of…
Chris
  • 3,265
  • 5
  • 37
  • 50
36
votes
3 answers

Reliability of atomic counters in DynamoDB

I was considering to use Amazon DynamoDB in my application, and I have a question regarding its atomic counters reliability. I'm building a distributed application that needs to concurrently, and consistently, increment/decrement a counter stored in…
Mark
  • 67,098
  • 47
  • 117
  • 162
36
votes
2 answers

Atomicity of loads and stores on x86

8.1.2 Bus Locking Intel 64 and IA-32 processors provide a LOCK# signal that is asserted automatically during certain critical memory operations to lock the system bus or equivalent link. While this output signal is asserted, requests from other…
Gilgamesz
  • 4,727
  • 3
  • 28
  • 63
36
votes
4 answers

How to use std::atomic<>

I have a class that I want to use in different threads and I think I may be able to use std::atomic this way: class A { int x; public: A() { x=0; } void Add() { x++; } void Sub() { x--; …
mans
  • 17,104
  • 45
  • 172
  • 321
36
votes
2 answers

When is a compiler-only memory barrier (such as std::atomic_signal_fence) useful?

The notion of a compiler fence often comes up when I'm reading about memory models, barriers, ordering, atomics, etc., but normally it's in the context of also being paired with a CPU fence, as one would expect. Occasionally, however, I read about…
etherice
  • 1,761
  • 15
  • 25
36
votes
10 answers

Which CPU architectures support Compare And Swap (CAS)?

just curious to know which CPU architectures support compare and swap atomic primitives?
oz10
  • 153,307
  • 27
  • 93
  • 128
36
votes
3 answers

Can a bool read/write operation be not atomic on x86?

Say we have two threads, one is reading a bool in a loop and another can toggle it at certain times. Personally I think this should be atomic because sizeof(bool) in C++ is 1 byte and you don't read/write bytes partially but I want to be 100%…
szx
  • 6,433
  • 6
  • 46
  • 67
36
votes
3 answers

Why is std::atomic much slower than volatile bool?

I've been using volatile bool for years for thread execution control and it worked fine // in my class declaration volatile bool stop_; ----------------- // In the thread function while (!stop_) { do_things(); } Now, since C++11 added…
user1773602