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 ).

2796 questions
55
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,401
  • 7
  • 35
  • 66
50
votes
1 answer

Is writing a reference atomic on 64bit VMs

The java memory model mandates that writing a int is atomic: That is, if you write a value to it (consisting of 4 bytes) in one thread and read it in another, you will get all bytes or none, but never 2 new bytes and 2 old bytes or such. This is not…
Steffen Heil
  • 3,958
  • 3
  • 29
  • 33
50
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,480
  • 3
  • 28
  • 61
47
votes
5 answers

Atomic UPSERT in SQL Server 2005

What is the correct pattern for doing an atomic "UPSERT" (UPDATE where exists, INSERT otherwise) in SQL Server 2005? I see a lot of code on SO (e.g. see Check if a row exists, otherwise insert) with the following two-part pattern: UPDATE ... FROM…
rabidpebble
  • 557
  • 1
  • 4
  • 8
47
votes
1 answer

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
  • 1,655
  • 5
  • 20
  • 39
47
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
  • 11,148
  • 13
  • 83
  • 169
47
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
  • 50,653
  • 19
  • 110
  • 158
45
votes
2 answers

SQL atomic increment and locking strategies - is this safe?

I have a question about SQL and locking strategies. As an example, suppose I have a view counter for the images on my website. If I have a sproc or similar to perform the following statements: START TRANSACTION; UPDATE images SET counter=counter+1…
Alexander Torstling
  • 16,682
  • 6
  • 55
  • 70
45
votes
1 answer

How to atomically update a maximum value?

In serial code, updating a maximum could be accomplished simply by template void update_maximum(T& maximum_value, T const& value) noexcept { if(value > maximum_value) maximum_value = value; } However, how should this be done for an…
Walter
  • 40,885
  • 16
  • 97
  • 176
44
votes
4 answers

Initializing std::atomic_bool?

I want to use std::atomic_bool because I want to have a boolean which is supposed to be accessed by different threads. It's a static member Variable. The Problem is that I want to initialize it with false as the first state. Normally I would do it…
Sapd
  • 577
  • 1
  • 5
  • 10
43
votes
3 answers

Double-Checked Lock Singleton in C++11

Is the following singleton implementation data-race free? static std::atomic m_instance; ... static Tp & instance() { if (!m_instance.load(std::memory_order_relaxed)) { std::lock_guard lock(m_mutex); if…
Nicola Bonelli
  • 7,673
  • 3
  • 23
  • 35
43
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
  • 15,964
  • 7
  • 60
  • 81
42
votes
1 answer

How to atomically negate an std::atomic_bool?

The naive boolean negation std::atomic_bool b; b = !b; does not seem to be atomic. I suspect this is because operator! triggers a cast to plain bool. How would one atomically perform the equivalent negation? The following code illustrates that the…
juanchopanza
  • 210,243
  • 27
  • 363
  • 452
42
votes
16 answers

Moving a directory atomically

I have two directories in the same parent directory. Call the parent directory base and the children directories alpha and bravo. I want to replace alpha with bravo. The simplest method is: rm -rf alpha mv bravo alpha The mv command is atomic,…
dirtside
  • 7,666
  • 9
  • 40
  • 52
42
votes
2 answers

Are atomic variables lock-free?

When we talk about atomic variables, such as C++11's atomic<>, is it lock free? Or is lock-freeness something different? If I manage a queue with atomic variables, will it be slower than a lock-free queue?
pythonic
  • 18,049
  • 33
  • 112
  • 196