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
29
votes
6 answers

When are lock free data structures less performant than mutual exclusion (mutexes)?

I read somewhere (can't find the page anymore) that lock free data structures are more efficient "for certain workloads" which seems to imply that sometimes they're actually slower or the gain from them can be zero in some situations. Taking the…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
29
votes
2 answers

Why are std::atomic objects not copyable?

It seems that std::atomic types are not copy constructible or copy assignable. Why? Is there a technical reason why copying atomic types is not possible? Or is the interface limited on purpose to avoid some sort of bad code?
TianyuZhu
  • 752
  • 6
  • 8
28
votes
1 answer

gcc atomic built-in functions

http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Atomic-Builtins.html I believe that the following code increases the value of var atomically. volatile int var = 0; __sync_fetch_and_add( &var, 1 ) I understood the above codes as the following…
ddoman
  • 1,051
  • 1
  • 10
  • 12
28
votes
4 answers

If volatile is useless for threading, why do atomic operations require pointers to volatile data?

I've been reading from many sources that the volatile keyword is not helpful in multithreaded scenarios. However, this assertion is constantly challenged by atomic operation functions that accept volatile pointers. For instance, on Mac OS X, we have…
zneak
  • 134,922
  • 42
  • 253
  • 328
28
votes
3 answers

On a multicore x86, is a LOCK necessary as a prefix to XCHG?

If mem is a shared memory location, do I need: XCHG EAX,mem or: LOCK XCHG EAX,mem to do the exchange atomically? Googling this yields both yes and no answers. Does anyone know this definitively?
Walter Bright
  • 4,277
  • 1
  • 23
  • 28
28
votes
2 answers

How can I create an atomic enum in C++?

Class atomic contains atomic versions of many different variable types. However, it doesn't contain an atomic enum type. Is there a way to use atomic enums or make my own? As far as I can tell, my only option is to either not use enums or use…
Cerran
  • 2,087
  • 2
  • 21
  • 33
28
votes
5 answers

How do you implement Software Transactional Memory?

In terms of actual low level atomic instructions and memory fences (I assume they're used), how do you implement STM? The part that's mysterious to me is that given some arbitrary chunk of code, you need a way to go back afterwards and determine if…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
28
votes
4 answers

Can volatile but unfenced reads yield indefinitely stale values? (on real hardware)

In answering this question a further question about the OP's situation came up that I was unsure about: it's mostly a processor architecture question, but with a knock-on question about the C++ 11 memory model as well. Basically, the OP's code was…
Stephen Lin
  • 5,470
  • 26
  • 48
27
votes
2 answers

should LOCK_EX on both read & write be atomic?

file_put_contents ( "file", "data", LOCK_EX ) for writing (which means - aquire lock and write) file_get_contents ( "file", LOCK_EX ) for reading (which means - aquire lock and then read) will it throw exception? raise an error? block until lock…
Kamil Tomšík
  • 2,415
  • 2
  • 28
  • 32
27
votes
2 answers

std::atomic with custom class (C++ 11)

I am using std::atomic with a custom class in my library. All works fine with MSVC, but now that i'm trying to get it to run on macOS, i get a linker error: undefined symbols for architecture x86_64: "__atomic_store", referenced from: _main in…
yvan vander sanden
  • 955
  • 1
  • 12
  • 13
27
votes
4 answers

Error with copy constructor/assignment operator for a class which has std::atomic member variable

I have a class like below. #include static const long myValue = 0; class Sequence { public: Sequence(long initial_value = myValue) : value_(initial_value) {} private: std::atomic value_; }; int main() { …
polapts
  • 5,493
  • 10
  • 37
  • 49
26
votes
4 answers

RESTful atomic update of multiple resources?

Imagine a web-application storing some data-resource with some id which stores three attachment (e.g. pdf) per datum. The URL scheme is data/{id}/attachment1 data/{id}/attachment2 data/{id}/attachment3 An RESTful API exists for the attachments…
mtsz
  • 2,725
  • 7
  • 28
  • 41
26
votes
3 answers

std::atomic decrement and comparison

On the following code: std::atomic myint; //Shared variable //(...) if( --myint == 0) { //Code block B } Is it possible that more than one thread access the block I named "Code Block B"? Please consider that overflow will not happen, that…
André Puel
  • 8,741
  • 9
  • 52
  • 83
26
votes
4 answers

Is the "switch" statement evaluation thread-safe?

Consider the following sample code: class MyClass { public long x; public void DoWork() { switch (x) { case 0xFF00000000L: // do whatever... break; case 0xFFL: …
Mario Vernari
  • 6,649
  • 1
  • 32
  • 44
26
votes
2 answers

Mixing C++11 atomics and OpenMP

OpenMP has its own support for atomic access, however, there are at least two reasons for preferring C++11 atomics: they are significantly more flexible and they are part of the standard. On the other hand, OpenMP is more powerful than the C++11…
user1494080
  • 2,064
  • 2
  • 17
  • 36