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
3
votes
5 answers

Volatile and atomic operation in java

I have read article concerning atomic operation in Java but still have some doubts needing to be clarified: int volatile num; public void doSomething() { num = 10; // write operation System.out.println(num) // read num = 20; // write …
MinhHoang
  • 681
  • 3
  • 9
  • 22
3
votes
2 answers

What is faster in CUDA: global memory write + __threadfence() or atomicExch() to global memory?

Assuming that we have lots of threads that will access global memory sequentially, which option performs faster in the overall? I'm in doubt because __threadfence() takes into account all shared and global memory writes but the writes are coalesced.…
dsilva.vinicius
  • 345
  • 2
  • 13
3
votes
3 answers

Atomic compare, Multi-Processor, C/C++ (Linux)

I have a variable in shared memory x on a multi-processor system. void MyFunction(volatile int* x) { if (*x != 0) { // do something } } Other processes (possibly on different processors) will be writing to x using gcc built-in atomic…
Switch
  • 5,126
  • 12
  • 34
  • 40
3
votes
1 answer

Can boost::atomic really improve performance by reducing overhead of sys calls (in mutex/semaphore) in multithreading?

I am trying to compare the performance of boost::atomic and pthread mutex on Linux: pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER ; int g = 0 ; void f() { pthread_mutex_lock(&mutex); ++g; pthread_mutex_unlock(&mutex); …
user1000107
  • 405
  • 7
  • 14
3
votes
2 answers

how to use boost atomic to remove race condition?

I am trying to use boost::atomic to do multithreading synchronization on linux. But, the result is not consistent. Any help will be appreciated. thanks #include #include #include…
user1000107
  • 405
  • 7
  • 14
3
votes
3 answers

Is AtomicInteger fair?

Does AtomicInteger provide any kind of fairness guarantee? like first-come-first-serve execution order of threads on it? The animated example at Victor Grazi's concurrent animated definitely does not show any such fairness. I've searched and haven't…
Rohan Grover
  • 1,514
  • 2
  • 17
  • 23
3
votes
3 answers

Lock free atomic state class - is it correct?

I am just looking for feedback (obvious flaws/ways to improve it) on my attempt to implement atomic read/writes on a structure. There will be one writing thread and multiple reading threads. The aim is to prevent a reader from getting an…
willcode.co
  • 674
  • 1
  • 7
  • 17
3
votes
2 answers

Race condition for shared variable

I have a shared variable of type double. This variable will be accessed by two threads. One thread will ever only write the variable, whereas the other thread will ever only read the variable. Do I still get race condition here? If yes, is there an…
Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48
2
votes
3 answers

GCC 4.7, including

I've just compiled GCC 4.7 to work with stdatomic.h, but I can't seem to -I it. stdatomic.h seems to live in /usr/include/c++/4.4.3, but then the linker tells me it needs a bunch of other files in dirs nearby. If I -I all of them, I still get the…
Dervin Thunk
  • 19,515
  • 28
  • 127
  • 217
2
votes
1 answer

PostgreSQL - do transactions ensure atomicity?

I want to do something like this: SELECT * FROM TABLE where *condition* ... TEST for a row being returned IF NOT INSERT the row In other words I only want to insert a row into the table if it is not already there. My concern is that while I am…
user911625
2
votes
1 answer

How correctly wake up process inside interrupt handlers

Briefly, in a read method i check if a variable is 0 and if it's i put the current process to sleep: static ssize_t soc2e_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos) { ... struct soc2e_dev…
MirkoBanchi
  • 2,173
  • 5
  • 35
  • 52
2
votes
2 answers

Shared memory mutex with CUDA - adding to a list of items

My problem is the following: I have an image in which I detect some points of interest using the GPU. The detection is a heavyweight test in terms of processing, however only about 1 in 25 points pass the test on average. The final stage of the…
Robotbugs
  • 4,307
  • 3
  • 22
  • 30
2
votes
1 answer

Mongoid+Rails: Atomic callbacks?

I know that in Rails all ActiveRecord callbacks are executed inside transaction. So if something goes wrong nothing is changed/wrong. Will Mongoid gem handle this in the same way (no transactions available as far as I know)? Thx!
xpepermint
  • 35,055
  • 30
  • 109
  • 163
2
votes
2 answers

Workings of AtomicReferenceArray

I am wondering if AtomicReferenceArray can be used as a replacement for ConcurrentLinkedQueue (if one could live with a bounded structure). I currently have something like: ConcurrentLinkedQueue queue = new…
CaptainHastings
  • 1,557
  • 1
  • 15
  • 32
2
votes
1 answer

Is moving a file atomic if more than 1 directory is involved?

In NTFS, file moving is atomic as explained here: [...] if you are running under NTFS then file operations are atomic at the file system level. A rename will occur in a single operation as far as any higher code is concerned. Does this mean…
mafu
  • 31,798
  • 42
  • 154
  • 247