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

difference between standard's atomic bool and atomic flag

I wasn't aware of the std::atomic variables but was aware about the std::mutex (weird right!) provided by the standard; however one thing caught my eye: there are two seemingly-same (to me) atomic types provided by the standard, listed…
hg_git
  • 2,884
  • 6
  • 24
  • 43
66
votes
3 answers

Django - Rollback save with transaction atomic

I am trying to create a view where I save an object but I'd like to undo that save if some exception is raised. This is what I tried: class MyView(View): @transaction.atomic def post(self, request, *args, **kwargs): try: …
Gocht
  • 9,924
  • 3
  • 42
  • 81
62
votes
3 answers

What operations are atomic in C#?

Is there a systematic way to know whether an operation in C# will be atomic or not? Or are there any general guidelines or rules of thumb?
Eric
  • 5,842
  • 7
  • 42
  • 71
61
votes
2 answers

writeToFile:atomically: what does atomically mean?

I am wondering what the atomically: parameter stands for in the writeToFile:atomically: method (-[NSArray writeToFile:atomically:] for example). It is common to pass YES for atomically:, but I don't know what it means.
JonasG
  • 9,274
  • 12
  • 59
  • 88
61
votes
4 answers

Are C/C++ fundamental types atomic?

Are C/C++ fundamental types, like int, double, etc., atomic, e.g. threadsafe? Are they free from data races; that is, if one thread writes to an object of such a type while another thread reads from it, is the behavior well-defined? If not, does it…
Carmellose
  • 4,815
  • 10
  • 38
  • 56
61
votes
2 answers

Does std::atomic work appropriately?

I am reading through Anthony Williams' "C++ Concurrency in Action" and in Chapter 5, which talks about the new multithreading-aware memory model and atomic operations, and he states: In order to use std::atomic for some user-defined UDT, this…
Thomas Russell
  • 5,870
  • 4
  • 33
  • 68
60
votes
2 answers

Django nested transactions - “with transaction.atomic()”

I would like to know if I have something like this: def functionA(): with transaction.atomic(): #save something functionB() def functionB(): with transaction.atomic(): #save another thing Someone knows what will…
Lara
  • 2,170
  • 6
  • 22
  • 43
60
votes
5 answers

AtomicInteger.incrementAndGet() vs. AtomicInteger.getAndIncrement()

When return value is not of interest, is there any (even irrelevant in practice) difference between AtomicInteger.getAndIncrement() and AtomicInteger.incrementAndGet() methods, when return value is ignored? I'm thinking of differences like which…
hyde
  • 60,639
  • 21
  • 115
  • 176
58
votes
5 answers

When is it preferable to use volatile boolean in Java rather than AtomicBoolean?

I've looked at the other volatile vs. Atomicxxxx questions in SO (including this one) and have read the description of java.util.current.atomic, and I am not quite satisfied with the nuances. If I'm trying to decide between using volatile boolean…
Jason S
  • 184,598
  • 164
  • 608
  • 970
58
votes
5 answers

Why is integer assignment on a naturally aligned variable atomic on x86?

I've been reading this article about atomic operations, and it mentions 32-bit integer assignment being atomic on x86, as long as the variable is naturally aligned. Why does natural alignment assure atomicity?
timlyo
  • 2,086
  • 1
  • 23
  • 35
58
votes
2 answers

Does it make any sense to use the LFENCE instruction on x86/x86_64 processors?

Often in internet I find that LFENCE makes no sense in processors x86, ie it does nothing , so instead MFENCE we can absolutely painless to use SFENCE, because MFENCE = SFENCE + LFENCE = SFENCE + NOP = SFENCE. But if LFENCE does not make sense, then…
Alex
  • 12,578
  • 15
  • 99
  • 195
57
votes
1 answer

Which std::sync::atomic::Ordering to use?

All the methods of std::sync::atomic::AtomicBool take a memory ordering (Relaxed, Release, Acquire, AcqRel, and SeqCst), which I have not used before. Under what circumstances should these values be used? The documentation uses confusing “load” and…
yonran
  • 18,156
  • 8
  • 72
  • 97
56
votes
9 answers

How to perform atomic operations on Linux that work on x86, arm, GCC and icc?

Every Modern OS provides today some atomic operations: Windows has Interlocked* API FreeBSD has Solaris has Mac OS X has Anything like that for Linux? I need it to work on most Linux supported…
Artyom
  • 31,019
  • 21
  • 127
  • 215
54
votes
7 answers

Avoid duplicate POSTs with REST

I have been using POST in a REST API to create objects. Every once in a while, the server will create the object, but the client will be disconnected before it receives the 201 Created response. The client only sees a failed POST request, and tries…
geon
  • 8,128
  • 3
  • 34
  • 41
53
votes
4 answers

Is rename() atomic?

I am not being able to check this via experiments and could not gather it from the man pages as well. Say I have two processes, one moving(rename) file1 from directory1 to directory2. Say the other process running concurrently copies the contents…
Lipika Deka
  • 3,774
  • 6
  • 43
  • 56