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
1
vote
1 answer

Is it safe to cast atomic to T

I have two questions: In the general case is it safe to use an atomic as a T and switch between them interchangeably? In the case of a futex is it safe to do the cast? I am aware that performing atomic operations on non-atomic types is undefined…
Ebony Ayers
  • 153
  • 1
  • 6
1
vote
1 answer

Ref Updates And Fiber Triggers Using Cats Effect

Problem: I am trying to solve a problem where I need to schedule for every x minutes, I need to update the cache and concurrent gets are possible. Solutions tried: Using TrieMap and ScheduledThreadPool Executor With Cats Effects: I actually…
Shankar Shastri
  • 1,031
  • 10
  • 17
1
vote
1 answer

If Atomic is used, is there a lock contention

This is not specific to any language. Just want to know the "lock contention" terms better. Let us say: we want to execute two kinds of commands using ARM instructions We have 10 stores, each sells product, there is one central boss holding a ledger…
WriteBackCMO
  • 597
  • 1
  • 6
  • 15
1
vote
1 answer

what do vulkan memory semantic flags like gl_SemanticsRelaxed, gl_SemanticsRelease, and gl_SemanticsAcquire do in Vulkan GLSL?

I'm trying to figure how the atomic store functions in the following code work, they rely on GL_KHR_memory_scope_semantics. uint exclusive_prefix = 0; if (local_ix == 31) { atomicStore(work_buf[my_tile * 4 + 2], total, gl_ScopeDevice,…
Krupip
  • 3,570
  • 1
  • 27
  • 40
1
vote
2 answers

memory_order_relaxed and visibility

Consider two threads, T1 and T2, that store and load an atomic integer a_i respectively. And let's further assume that the store is executed before the load starts being executed. By before, I mean in the absolute sense of time. T1 …
cmutex
  • 953
  • 5
  • 14
1
vote
1 answer

What is processor Lock# signal and how it works?

I' was reading a book about assembly(intermediate level) and it mentioned that some instructions like xchg automatically assert the processor LOCK# signal. Searching online about it revealed that it give the processor the exclusive right of any…
Khaled
  • 916
  • 3
  • 15
1
vote
1 answer

How is this a guarantee a value has been atomically updated in ARM?

ARM provides LDREX/STREX to atomically load/store values, but I feel like I'm missing something in how this is still an atomic operation. The following below is generally how an increment by one would be done. However, what's preventing something…
1
vote
1 answer

c++ release fence with atomic acquire operation example

I'm looking for a example on synchronization regarding a release fence and a atomic acquire operation. All release fence examples i have found are fence-fence synchronization.
TheNegative
  • 161
  • 3
  • 7
1
vote
1 answer

Implementation of a lock free vector

After several searches, I cannot find a lock-free vector implementation. There is a document that speaks about it but nothing concrete (in any case I have not found it). http://pirkelbauer.com/papers/opodis06.pdf There are currently 2 threads…
antho
  • 301
  • 2
  • 13
1
vote
0 answers

How to ensure a piece of code is always used by one thread at any given time?

I'm writing a function that needs to use mutable static variables to work (a weird implementation of a message loop). In order to allow only one writer to access those variables at any given time, I need to make the access to this function exclusive…
Gymore
  • 465
  • 1
  • 10
1
vote
0 answers

How do I initialize a Vec from multiple threads?

I have code that generates a CSR sparse matrix by reading multiple parquet files in parallel, preprocessing the data, then finally acquiring a mutex to sequentially write into the sparse array data structure. This is roughly the pseudocode for each…
grasevski
  • 2,429
  • 1
  • 19
  • 21
1
vote
2 answers

What happens when two semaphores change simultaneously and one of the two cannot decrement immediately?

Consider this code: struct sembuf s_op[2]; s_op[0].sem_num = old; s_op[0].sem_op = 1; s_op[0].sem_flg = 0; s_op[1].sem_num = new; s_op[1].sem_op = -1; s_op[1].sem_flg = 0; semop(semid, s_op, 2); Now, if "new" cannot be decremented immediately…
Bers
  • 74
  • 7
1
vote
1 answer

OpenCL - atomic_cmpxchg

What does this function do??. I couldn't understand a thing from the OpenCL specification!! The code below is a snippet from spMV code. atomic_cmpxchg((__global int*)loc, *((int*)&old), *((int*)&sum)) != *((int*)&old)
Jay
  • 11
  • 2
1
vote
1 answer

Feasibility of java.util.concurrent.atomic.LongAdder class

LongAdder is a cunningly designed atomic counter which is supposed to reduce cache line contention when updating a shared counter. The problem is, it relies on atomic CAS operation to actually update the count (this is a trait it shares with a more…
oakad
  • 5,905
  • 16
  • 27
1
vote
1 answer

Avoiding race condition on Nodejs Api

I'm using a Nodejs Api server and facing a special situation where a bunch of users notify me with a boolean indication, and only when all users sent me the indication, I'm then calling a method to do some job. So for the example, I create a group…
Shalashka
  • 47
  • 5
1 2 3
99
100