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

Avoid getting released objects from concurrently used accessors

In my multithreaded application, there is a property that can be accessed concurrently by multiple threads. The property is defined as @property (retain) NSObject *status. How do I atomically get and retain the property, so I can safely use it in…
Twilite
  • 873
  • 9
  • 22
2
votes
2 answers

Turning onMessage() method into an atomic action

I've encounter the problem that if my method below fails or it's an exception I still consume the msg. I would want the functionality to do a rollback during the catch and place the msg back on the queue/topic. public void onMessage(Message…
Will
  • 8,246
  • 16
  • 60
  • 92
2
votes
1 answer

Is it possible to atomically read a double word with single-word operations?

I remember that at some point, I saw a programming challenge where people were asked to atomically read a double word with single-word operations. Now, before we go on, a few clarifications: A single word is a unit that has the natural number of…
zneak
  • 134,922
  • 42
  • 253
  • 328
2
votes
1 answer

InterlockedExchange Visual Studio 2010 Intrinsic

I have intrinsics enabled in the optimization settings for the compiler, however, the resulting code for InterlockedExchange is generating calls into kernel32.dll rather than producing inline assembly. This is especially problematic because the…
user1157123
2
votes
4 answers

Java's AtomicLong implementation loops

So I was using AtomicLong and decided to have a look at its implementation, and noticed the following difference between 2 methods: getAndAdd(long delta): public final long getAndAdd(long delta) { while (true) { long current = get(); …
tmbrggmn
  • 8,680
  • 10
  • 35
  • 44
2
votes
2 answers

Move file in Linux only when it's not in use by another process

Using the lsof command, I can determine whether a file is in use by some process, but I need to atomically check a file for use and move it only if unused. These files are in use by various other programs over which I have no control, so I can't use…
Jegschemesch
  • 11,414
  • 4
  • 32
  • 37
2
votes
1 answer

C++11 Thread-safety with stack variables

I'm getting a bit confused with the new threading in C++11. I get how I can use mutexes to stop two threads from operating on the same data at the same time, but what about assigning to that data? Example! class Foo { std::string s; // This…
Joe
  • 726
  • 1
  • 8
  • 18
2
votes
2 answers

"atomic" operation desturbed by asynchronous ajax callbacks

I know, using the words JavaScript and 'atomic'-anything in the same sentence is kinda strange, since JavaScript is prised to be asynchronous and therefor not very atomic. //EDIT This was a mistake on my side! by having alert's going off (and hiding…
japrescott
  • 4,736
  • 3
  • 25
  • 37
2
votes
2 answers

How to ensure atomic reads and atomic writes?

I want atomic reads and atomic writes to Integer (not int) and others as follows: volatile Double a; // not double volatile Long b; // not long Integer c; // not int Boolean d; // not boolean If not, how would I make them atomic? Edit: As a…
H2ONaCl
  • 10,644
  • 14
  • 70
  • 114
2
votes
1 answer

Atomic Operation in Java to create a hardlink and delete a file

I am writing a code in JDK 7 on Unix which compares two files. If both files are same say File A and File B. Then it should delete File B and create a hardlink to File A. Simple way is : 1. Compare if files are same a. delete File B b. …
amit modi
  • 1,098
  • 1
  • 11
  • 26
2
votes
1 answer

Optimistic locking strategy with atomics in C++ and ordering

After reading up on c++0x's atomics and in combination with non locking queues I decided to have a go at playing with them. The idea was to write a single producer, multiple consumer queue with optimistic locking. The messages do not need to be…
2
votes
1 answer

How to call ioctl with commands SIOCGIFFLAGS and SIOCSIFFLAGS atomically

Is there a way to call ioctl (the question is also valid for all sys calls) with commands SIOCGIFFLAGS and SIOCSIFFLAGS in an atomic manner? For example if i would add the IFF_PROMISC flag to an interface: ... struct ifreq ifr; memset(&ifr, 0,…
MirkoBanchi
  • 2,173
  • 5
  • 35
  • 52
2
votes
3 answers

thread safe data exchange between threads/shared memory in C++ on linux

I got a "bit" confused: In production we have two processes communicating via shared memory, a part of data exchange is a long and a bool. The access to this data is not synchronized. It's been working fine for a long time and still is. I know…
Tadzys
  • 1,044
  • 3
  • 16
  • 22
2
votes
1 answer

How to write atomic RMW-sequences on Cortex-M4 in C++

In the following example are 4 versions to atomically increment (or use other form of rmw-statements) on a variable a1 or a2 (depending on the version). The variable a1 or a2 may be shared with some form of ISR. The question is according to a…
wimalopaan
  • 4,838
  • 1
  • 21
  • 39
2
votes
2 answers

Is it still atomic if the desired value of std::atomic::compare_exchange_weak is return of a non-atomic operation?

Does head.load()->next break the atomicity here for the std::atomic::compare_exchange_weak used in while(head && !head.compare_exchange_weak(ptr, head.load()->next)); ? I believe it should, but again it does not seem so in the output (which is below…
1 2 3
99
100