Is assignment to a std::atomic<double> variable guaranteed to be atomic?

tesla1060

I have the following scenario

// first assigned a value greater than 0
std::atomic<double> var=1;

//thread 1
while (True) {
    var = 1;
}

//thread 2
if(var >0) {
    //do something
}

My question is when thread 1 doing assignment, is there a possibility thread 2 sees var other than 1? Possibly some random number or zero?

It seems to happen in my production build with O2 optimization(the above snippet is not the real code, O2 may optimize most away). It seems, according to the standard, that this should never happen.

Is my description correct, and the bug is elsewhere? Or am I missing something?

I am using c++17, gcc9.3.1

Ryan M

Yes, writes to atomic types are atomic. The bug in your code is elsewhere.

Because it has never been set to anything other than 1, and the additional writes of 1 are atomic, there can't be any sort of "split read" where it reads some other value (as can happen on some architectures where the value may be written across multiple instructions, and a read might see the high bits but not the low bits).

This would be true even if the original number weren't 1, as the reading thread will either see the entire write or not see the write at all (the definition of an atomic write).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

are reads and writes to a variable of type double guaranteed to be atomic on a 64 bit intel processor?

From Dev

Is a zero initialized std::atomic<T*> guaranteed equivalent to one initialized with nullptr?

From Dev

Why only std::atomic_flag is guaranteed to be lock-free?

From Dev

Is a read on an atomic variable guaranteed to acquire the current value of it in C++11?

From Dev

atomic read then write with std::atomic

From Dev

std::atomic_flag as member variable

From Dev

Using std::condition_variable with atomic<bool>

From Dev

Why is integer assignment on a naturally aligned variable atomic on x86?

From Dev

Sync is unreliable using std::atomic and std::condition_variable

From Java

Where is the lock for a std::atomic?

From Java

What exactly is std::atomic?

From Dev

Segfault in std::atomic load?

From Dev

Correct usage of std::atomic

From Dev

Atomic operations, std::atomic<> and ordering of writes

From Dev

Java multi threading atomic assignment

From Dev

Why is reference assignment atomic in Java?

From Dev

is reference assignment atomic in Swift 5?

From Dev

Java multi threading atomic assignment

From Dev

std::atomic as a value of std::map

From Dev

Passing an atomic variable to a function

From Dev

How to predefine <atomic> variable?

From Dev

Is double read atomic on an Intel architecture?

From Dev

OpenCL - using atomic reduction for double

From Dev

std::atomic_store and std::atomic_exchange do not exchange

From Dev

use of deleted function - std::atomic

From Dev

Comparison semantics with std::atomic types

From Dev

Using std::atomic with aligned classes

From Dev

Using std::atomic from C

From Dev

std::atomic<> operator++ in MSVC

Related Related

HotTag

Archive