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
33
votes
12 answers

Are +=, |=, &= etc atomic?

Are the "modify" operators like +=, |=, &= etc atomic? I know ++ is atomic (if you perform x++; in two different threads "simultaneously", you will always end up with x increased by 2, as opposed to x=x+1 with optimization switched off.) What I…
SF.
  • 13,549
  • 14
  • 71
  • 107
33
votes
4 answers

Thread-safe cache libraries for .NET

Background: I maintain several Winforms apps and class libraries that either could or already do benefit from caching. I'm also aware of the Caching Application Block and the System.Web.Caching namespace (which, from what I've gathered, is…
Aaronaught
  • 120,909
  • 25
  • 266
  • 342
33
votes
6 answers

Atomic Operations and multithreading

Recently I was reading a tutorial, in that I came across a statement that says.. "The Java language specification guarantees that reading or writing a variable is an atomic operation(unless the variable is of type long or double). Operations…
MaheshVarma
  • 2,081
  • 7
  • 35
  • 58
33
votes
7 answers

UNIX Portable Atomic Operations

Is there a (POSIX-)portable way in C for atomic variable operations similar to a portable threading with pthread? Atomic operations are operations like "increment and get" that are executed atomically that means that no context switch can interfere…
dmeister
  • 34,704
  • 19
  • 73
  • 95
32
votes
4 answers

C++ std::atomic vs. Boost atomic

In my application, I have an int and a bool variable, which are accessed (multiple write/read) by multiple threads. Currently, I am using two mutexes, one for int and one for bool to protect those variables. I heard about using atomic variables and…
2607
  • 4,037
  • 13
  • 49
  • 64
32
votes
3 answers

Can atomics suffer spurious stores?

In C++, can atomics suffer spurious stores? For example, suppose that m and n are atomics and that m = 5 initially. In thread 1, m += 2; In thread 2, n = m; Result: the final value of n should be either 5 or 7, right? But could it…
thb
  • 13,796
  • 3
  • 40
  • 68
32
votes
2 answers

Is it possible to create an atomic vector or array in C++?

I have some code which uses an array of int (int[]) in a thread which is activated every second. I use lock() from std::mutex to lock this array in this thread. However I wonder if there is a way to create an atomic array (or vector) to avoid using…
rainbow
  • 1,161
  • 3
  • 14
  • 29
32
votes
2 answers

Why isn't atomic double fully implemented

My question is quite simple. Why isn't std::atomic implemented completely? I know it has to do with atomic RMW (read-modify-write) access. But I really don't see, why this shouldn't be possible on a double. It's specified that any trivially…
laurisvr
  • 2,724
  • 6
  • 25
  • 44
32
votes
1 answer

what is "failure atomicity" used by J bloch and how its beneficial in terms of immutable object?

just came across below statement as benefit of immutable object Immutable object always have “failure atomicity” (a term used by Joshua Bloch) : if an immutable object throws an exception, it’s never left in an undesirable or…
Bhargav Modi
  • 2,605
  • 3
  • 29
  • 49
32
votes
3 answers

Is a variable swap guaranteed to be atomic in python?

With reference to the following link: http://docs.python.org/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe I wanted to know if the following: (x, y) = (y, x) will be guaranteed atomic in cPython. (x and y are both python…
dhruvbird
  • 6,061
  • 6
  • 34
  • 39
31
votes
5 answers

Relative performance of swap vs compare-and-swap locks on x86

Two common locking idioms are: if (!atomic_swap(lockaddr, 1)) /* got the lock */ and: if (!atomic_compare_and_swap(lockaddr, 0, val)) /* got the lock */ where val could simply be a constant or an identifier for the new prospective owner of the…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
31
votes
4 answers

Why is there no overload of Interlocked.Add that accepts Doubles as parameters?

I fully appreciate the atomicity that the Threading.Interlocked class provides; I don't understand, though, why the Add function only offers two overloads: one for Integers, another for Longs. Why not Doubles, or any other numeric type for that…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
30
votes
2 answers

What does the Atomics object do in JavaScript?

This is the MDN documentation on Atomics. I can't understand how an atomic object could be used in a real-life situation or if they are for internal use only (i.e. not meant to be called at all). What does the Atomics object do in JavaScript? How…
adelriosantiago
  • 7,762
  • 7
  • 38
  • 71
29
votes
2 answers

Memory model ordering and visibility?

I tried looking for details on this, I even read the standard on mutexes and atomics... but still I couldnt understand the C++11 memory model visibility guarantees. From what I understand the very important feature of mutex BESIDE mutual exclusion…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
29
votes
4 answers

What is the difference between std::shared_ptr and std::experimental::atomic_shared_ptr?

I read the following article by Antony Williams and as I understood in addition to the atomic shared count in std::shared_ptr in std::experimental::atomic_shared_ptr the actual pointer to the shared object is also atomic? But when I read about…
bobeff
  • 3,543
  • 3
  • 34
  • 62