Are C++ Reads and Writes of an int Atomic?

C++MultithreadingSynchronization

C++ Problem Overview


I have two threads, one updating an int and one reading it. This is a statistic value where the order of the reads and writes is irrelevant.

My question is, do I need to synchronize access to this multi-byte value anyway? Or, put another way, can part of the write be complete and get interrupted, and then the read happen.

For example, think of a value = 0x0000FFFF that gets incremented value of 0x00010000.

Is there a time where the value looks like 0x0001FFFF that I should be worried about? Certainly the larger the type, the more possible something like this to happen.

I've always synchronized these types of accesses, but was curious what the community thinks.

C++ Solutions


Solution 1 - C++

Boy, what a question. The answer to which is:

> Yes, no, hmmm, well, it depends

It all comes down to the architecture of the system. On an IA32 a correctly aligned address will be an atomic operation. Unaligned writes might be atomic, it depends on the caching system in use. If the memory lies within a single L1 cache line then it is atomic, otherwise it's not. The width of the bus between the CPU and RAM can affect the atomic nature: a correctly aligned 16bit write on an 8086 was atomic whereas the same write on an 8088 wasn't because the 8088 only had an 8 bit bus whereas the 8086 had a 16 bit bus.

Also, if you're using C/C++ don't forget to mark the shared value as volatile, otherwise the optimiser will think the variable is never updated in one of your threads.

Solution 2 - C++

At first one might think that reads and writes of the native machine size are atomic but there are a number of issues to deal with including cache coherency between processors/cores. Use atomic operations like Interlocked* on Windows and the equivalent on Linux. C++0x will have an "atomic" template to wrap these in a nice and cross-platform interface. For now if you are using a platform abstraction layer it may provide these functions. ACE does, see the class template ACE_Atomic_Op.

Solution 3 - C++

IF you're reading/writing 4-byte value AND it is DWORD-aligned in memory AND you're running on the I32 architecture, THEN reads and writes are atomic.

Solution 4 - C++

Yes, you need to synchronize accesses. In C++0x it will be a data race, and undefined behaviour. With POSIX threads it's already undefined behaviour.

In practice, you might get bad values if the data type is larger than the native word size. Also, another thread might never see the value written due to optimizations moving the read and/or write.

Solution 5 - C++

You must synchronize, but on certain architectures there are efficient ways to do it.

Best is to use subroutines (perhaps masked behind macros) so that you can conditionally replace implementations with platform-specific ones.

The Linux kernel already has some of this code.

Solution 6 - C++

On Windows, InterlockedExchangeAdd is guaranteed to be atomic.

Solution 7 - C++

To echo what everyone said upstairs, the language pre-C++0x cannot guarantee anything about shared memory access from multiple threads. Any guarantees would be up to the compiler.

Solution 8 - C++

No, they aren't (or at least you can't assume they are). Having said that, there are some tricks to do this atomically, but they typically aren't portable (see Compare-and-swap).

Solution 9 - C++

I agree with many and especially Jason. On windows, one would likely use InterlockedAdd and its friends.

Solution 10 - C++

Asside from the cache issue mentioned above...

If you port the code to a processor with a smaller register size it will not be atomic anymore.

IMO, threading issues are too thorny to risk it.

Solution 11 - C++

Lets take this example

int x;
x++;
x=x+5;

The first statement is assumed to be atomic because it translates to a single INC assembly directive that takes a single CPU cycle. However, the second assignment requires several operations so it's clearly not an atomic operation.

Another e.g,

x=5;

Again, you have to disassemble the code to see what exactly happens here.

Solution 12 - C++

tc, I think the moment you use a constant ( like 6) , the instruction wouldn't be completed in one machine cycle. Try to see the instruction set of x+=6 as compared to x++

Solution 13 - C++

Some people think that ++c is atomic, but have a eye on the assembly generated. For example with 'gcc -S' :

movl    cpt.1586(%rip), %eax
addl    $1, %eax
movl    %eax, cpt.1586(%rip)

To increment an int, the compiler first load it into a register, and stores it back into the memory. This is not atomic.

Solution 14 - C++

Definitively NO ! That answer from our highest C++ authority, M. Boost:
Operations on "ordinary" variables are not guaranteed to be atomic.

Solution 15 - C++

The only portable way is to use the sig_atomic_t type defined in signal.h header for your compiler. In most C and C++ implementations, that is an int. Then declare your variable as "volatile sig_atomic_t."

Solution 16 - C++

Reads and writes are atomic, but you also need to worry about the compiler re-ordering your code. Compiler optimizations may violate happens-before relationship of statements in your code. By using atomic you don't have to worry about that. ... atomic i;

soap_status = GOT_RESPONSE ; i = 1

In the above example, the variable 'i' will only be set to 1 after we get a soap response.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontheschmitzerView Question on Stackoverflow
Solution 1 - C++SkizzView Answer on Stackoverflow
Solution 2 - C++Adam MitzView Answer on Stackoverflow
Solution 3 - C++gabrView Answer on Stackoverflow
Solution 4 - C++Anthony WilliamsView Answer on Stackoverflow
Solution 5 - C++Jason CohenView Answer on Stackoverflow
Solution 6 - C++Andrew SteinView Answer on Stackoverflow
Solution 7 - C++Mat NoguchiView Answer on Stackoverflow
Solution 8 - C++Leon TimmermansView Answer on Stackoverflow
Solution 9 - C++kennyView Answer on Stackoverflow
Solution 10 - C++JeffVView Answer on Stackoverflow
Solution 11 - C++siddhusinghView Answer on Stackoverflow
Solution 12 - C++siddhusinghView Answer on Stackoverflow
Solution 13 - C++ethamView Answer on Stackoverflow
Solution 14 - C++Jean DavyView Answer on Stackoverflow
Solution 15 - C++Edward HoworkaView Answer on Stackoverflow
Solution 16 - C++coredumpView Answer on Stackoverflow