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
35
votes
2 answers

compare and swap vs test and set

Could someone explain to me the working and differences of above operations in multi-threading?
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
35
votes
3 answers

How to initialize a static std::atomic data member

I would like to generate identifiers for a class named order in a thread-safe manner. The code below does not compile. I know that the atomic types do not have copy constructors, and I assume that explains why this code does not work. Does anybody…
Teisman
  • 1,318
  • 2
  • 13
  • 26
35
votes
2 answers

Is C++11 atomic usable with mmap?

I want to add network control of a handful of parameters used by a service (daemon) running on a Linux embedded system. There's no need for procedure calls, each parameter can be polled in a very natural way. Shared memory seems a nice way to keep…
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
35
votes
12 answers

In C is "i+=1;" atomic?

In C, is i+=1; atomic?
Crazy Chenz
  • 12,650
  • 12
  • 50
  • 62
35
votes
1 answer

C++11 memory_order_acquire and memory_order_release semantics?

http://en.cppreference.com/w/cpp/atomic/memory_order, and other C++11 online references, define memory_order_acquire and memory_order_release as: Acquire operation: no reads in the current thread can be reordered before this load. Release…
Cedomir Segulja
  • 417
  • 1
  • 4
  • 6
34
votes
7 answers

SSE instructions: which CPUs can do atomic 16B memory operations?

Consider a single memory access (a single read or a single write, not read+write) SSE instruction on an x86 CPU. The instruction is accessing 16 bytes (128 bits) of memory and the accessed memory location is aligned to 16 bytes. The document "Intel®…
user811773
34
votes
8 answers

Atomicity in C++ : Myth or Reality

I have been reading an article about Lockless Programming in MSDN. It says : On all modern processors, you can assume that reads and writes of naturally aligned native types are atomic. As long as the memory bus is at least as wide as the…
ali_bahoo
  • 4,732
  • 6
  • 41
  • 63
34
votes
0 answers

Per-element atomicity of vector load/store and gather/scatter?

Consider an array like atomic shared_array[]. What if you want to SIMD vectorize for(...) sum += shared_array[i].load(memory_order_relaxed)?. Or to search an array for the first non-zero element, or zero a range of it? It's probably…
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
34
votes
2 answers

Does Django Atomic Transaction lock the database?

When you do: @transaction.atomic def update_db(): do_bulk_update() while the function is running, does it lock the database? I'm asking regarding django's atomic…
kong
  • 423
  • 1
  • 4
  • 12
34
votes
5 answers

Atomic properties vs thread-safe in Objective-C

In most of the discussions I've read, it indicates that making a property atomic does not guarantee it to be thread-safe, it just guarantees that the value returned won't be garbage as a result of one object writing to it and another trying to read…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
34
votes
5 answers

AtomicInteger and volatile

I know volatile allows for visibility, AtomicInteger allows for atomicity. So if I use a volatile AtomicInteger, does it mean I don't have to use any more synchronization mechanisms? Eg. class A { private volatile AtomicInteger count; …
Achow
  • 8,600
  • 6
  • 39
  • 49
34
votes
5 answers

What is the difference between using explicit fences and std::atomic?

Assuming that aligned pointer loads and stores are naturally atomic on the target platform, what is the difference between this: // Case 1: Dumb pointer, manual fence int* ptr; // ... std::atomic_thread_fence(std::memory_order_release); ptr = new…
Cameron
  • 96,106
  • 25
  • 196
  • 225
34
votes
7 answers

Which is threadsafe atomic or non atomic?

I searched and found immutable are thread safe while mutable is not. This is fine. But i got misleading notes, blogs, answers about atomic vs non-atomic about thread safety, kindly give an explanation for the answer. Suppose there is an atomic…
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
33
votes
1 answer

Lock-free Reference counting and C++ smart pointers

In general, most widely known implementations of reference-counting smart ptr classes in C++, including the standard std::shared_ptr, use atomic reference counting, but do not provide atomic access to the same smart ptr instance. In other words,…
Siler
  • 8,976
  • 11
  • 64
  • 124
33
votes
2 answers

Should std::atomic be volatile?

I'm running a thread that runs until a flag is set. std::atomic stop(false); void f() { while(!stop.load(std::memory_order_{relaxed,acquire})) { do_the_job(); } } I wonder if the compiler can unroll loop like this (I don't want it to…
Inbae Jeong
  • 4,053
  • 25
  • 38