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
52
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
  • 4,286
  • 3
  • 32
  • 35
52
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
  • 657
  • 1
  • 6
  • 11
50
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
  • 44,150
  • 20
  • 113
  • 196
49
votes
4 answers

How to declare a vector of atomic in C++

I am intending to declare a vector of atomic variables to be used as counters in a multithreaded programme. Here is what I tried: #include #include int main(void) { std::vector> v_a; std::atomic a_i(1); …
steffen
  • 8,572
  • 11
  • 52
  • 90
48
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
  • 18,552
  • 7
  • 62
  • 74
48
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
  • 567
  • 1
  • 5
  • 8
48
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
  • 20,589
  • 43
  • 136
  • 219
47
votes
2 answers

std::atomic | compare_exchange_weak vs. compare_exchange_strong

I'm unsure if it's me not understanding or the documentation isn't clearly formulated. The following excerpt has been taken from the newest draft (N3126, section 29.6): bool atomic_compare_exchange_weak(volatile A* object, C * expected, C…
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
47
votes
6 answers

Is incrementing a field in MySQL atomic?

I'm making a web site where I would like to increment a counter in a standard MyISAM table. Simplified example: UPDATE votes SET num = num + 1; Will this cause problems if multiple connections are doing the same query, or will MySQL take care of it…
Newb
  • 513
  • 1
  • 4
  • 7
47
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
  • 8,144
  • 10
  • 42
  • 54
46
votes
2 answers

Understanding c++11 memory fences

I'm trying to understand memory fences in c++11, I know there are better ways to do this, atomic variables and so on, but wondered if this usage was correct. I realize that this program doesn't do anything useful, I just wanted to make sure that…
jcoder
  • 29,554
  • 19
  • 87
  • 130
45
votes
2 answers

Acquire/Release versus Sequentially Consistent memory order

For any std::atomic where T is a primitive type: If I use std::memory_order_acq_rel for fetch_xxx operations, and std::memory_order_acquire for load operation and std::memory_order_release for store operation blindly (I mean just like resetting…
zahir
  • 1,298
  • 2
  • 15
  • 35
44
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
  • 223,364
  • 34
  • 402
  • 480
44
votes
1 answer

Use cases for updateOne over findOneAndUpdate in MongoDB

I think that findOneAndUpdate carries out an atomic operation, so I'm assuming that updateOne does not. Why would you choose updateOne over findOneAndUpdate and avoid an atomic operation and have to spend extra time checking if the updates were…
Nick Pineda
  • 6,354
  • 11
  • 46
  • 66
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
  • 8,101
  • 4
  • 26
  • 35