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
2
votes
1 answer

Force an atomic store to occur before other operations

If I have 2 threads that execute f() and f2(), like this: std::atomic_flag flag; // a member variable in a class std::mutex m; flag.test_and_set(); // set to true before f() or f2() // executed only once (a "stop" function) void f() { …
j0011
  • 23
  • 5
2
votes
1 answer

About c++ memory order: how to keep other threads to access common resources safely?

This is my code: Godbolt. #include #include #include #include int main(int, char **) { volatile bool resource = true; std::atomic_bool stop{false}; std::atomic_int working{0}; std::thread…
2
votes
1 answer

Is the "unique_lock& lock" parameter really unavoidable when using condition_variable with atomic_flag, or am I missing something?

What I have here is an std::jthread to run some tasks every 10 seconds, while allowing the thread to quit on signal during sleep, which would probably be something like this: void ThreadMain(std::condition_variable& cv, const std::atomic_flag&…
Agritite
  • 27
  • 6
2
votes
0 answers

Is INSERT-WHERE atomic?

Is the following SQL atomic? is there any risk for two rows with the same id? INSERT INTO [dbo].[table] (id, whatever) SELECT * FROM (SELECT '00000000-0000-0000-0000-000000000001' as id, 'something' as…
Tar
  • 8,529
  • 9
  • 56
  • 127
2
votes
1 answer

Clearing the next pointer might not be needed

In the book "C++ Concurrency in Action" by Anthony Williams, when describing the listing "7.9 A lock-free stack using a lock-free std::shared_ptr<>", the author states that we have to: to clear the next pointer from the popped node in order to…
Maurice
  • 31
  • 3
2
votes
1 answer

Delay in atomic variable update reflection across threads

I am interested in the exploring the minimum time in which some write in a variable can be reflected across threads. For this I am using a global atomic variable and updating it periodically. Meanwhile, another thread spins and checks for updated…
2
votes
0 answers

std::atomic with no conversion operators

I need to refactor some C++ code that needs some accesses to become atomic. std::atomic is nice, but can be assigned from and converted to value_type, which means that existing call sites are taken over without complaint. Is there a variant of…
Simon Richter
  • 28,572
  • 1
  • 42
  • 64
2
votes
1 answer

CUDA AtomicCAS Deadlock

I have an array matrix with values of 0, and I want to increment some of it's elements by 1. The indices of matrix which I want to increment are stored in array indices. I need to increment some elements several times, thus I'm trying to use an…
sergei
  • 23
  • 3
2
votes
0 answers

C++ Undefined Behavior in Boost lock-free freelist_stack?

It looks like there's undefined behavior in Boost freelist_stack, and I want to make sure I'm reasoning about this correctly. For a given node, it uses the node memory to hold the stored object (of type T) when the slot is used, and a freelist_node…
2
votes
1 answer

Disabled DCache will prevent atomic_flag from being set

We are using a zynq-7000 based CPU, so an cortex-a9 and we encountered the following issue while using atomic_flags which are inside an library we are using (open-amp). We are using the second CPU on the SoC to execute bare-metal code. When…
hellow
  • 12,430
  • 7
  • 56
  • 79
2
votes
0 answers

How can I make Clang optimize redundant read of atomic when its value is know ahead of time?

Here we have three similar functions. They all always return 0. This first function is optimized well. The compiler sees that x is always 1. int f1() { std::atomic x = 1; if (x.load() == 1) { return 0; } return…
janekb04
  • 4,304
  • 2
  • 20
  • 51
2
votes
0 answers

use atomic operations fail in ebpf

My environment: aarch64-linux-5.15.7 bpf compile tools: clang-11 bpf compile param: clang -g -target bpf -Wall -O2 -g3 -g -DBPF_CODE -ggdb -std=gnu99 -D__TARGET_ARCH_arm64 -Wno-compare-distinct-pointer-types Using libbpf…
Jefen
  • 21
  • 2
2
votes
1 answer

How does the memory controller guarantee memory ordering of atomics when propagating cachelines?

I'm currently taking a deep look at std::atomics and the C++ memory model. What really helped my mental model is the concept of the store and load buffer of the CPU, which is basically a fifo queue for data that has to be written or read to/from the…
glades
  • 3,778
  • 1
  • 12
  • 34
2
votes
1 answer

C11 memory fence and atomic operation

I'm studying about memory barriers. I have some questions about following code. //version 1 Thread A: *val = 1; atomic_thread_fence(memory_order_release); atomic_store_explicit(published, 1, memory_order_relaxed); Thread B: if…
ehow
  • 21
  • 1
2
votes
0 answers

what is the best way to synchronize access to a map, while usage is only reading from the map, and replacing the map (no write of new entries)

I am implementing a mechanism in which I have a map that is only read by multiple go routines (entry = myMap["b"]), or entirely replaced, by a single go routine (myMap = myNewMap). According to this well answered question, and go documentation,…
1 2 3
99
100