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
28 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
  • 178,991
  • 47
  • 309
  • 337
7 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,…
  • 3,541
  • 3
  • 13
  • 4
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…
  • 9,141
  • 7
  • 32
  • 48
13 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.
  • 19,313
  • 27
  • 97
  • 155
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…
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 …
Leo Heinsaar
  • 3,887
  • 3
  • 15
  • 35
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
  • 2,001
  • 2
  • 13
  • 11
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
  • 41,187
  • 18
  • 82
  • 120
6 answers

When do I really need to use atomic instead of bool?

Isn't atomic redundant because bool is atomic by nature? I don't think it's possible to have a partially modified bool value. When do I really need to use atomic instead of bool?
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
  • 9,075
  • 11
  • 44
  • 55
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
  • 47,586
  • 132
  • 466
  • 875
8 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++; ?
  • 13,460
  • 29
  • 98
  • 167
4 answers

reference assignment is atomic so why is Interlocked.Exchange(ref Object, Object) needed?

In my multithreaded asmx web service I had a class field _allData of my own type SystemData which consists of few List and Dictionary marked as volatile. The system data (_allData) is refreshed once in a while and I do it by creating another…
char m
  • 7,840
  • 14
  • 68
  • 117
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…
  • 1,199
  • 2
  • 8
  • 3
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…
  • 90,338
  • 53
  • 357
  • 513
2 3
99 100