3

So does operations on char are atomic? So in thread A I read char T and in thread B I write on same char T, are these standard operations atomic?

char a;

#thread A
{ 
if(a & 0x01)
  ...

}

#thread B
{ 
 a =0x01;
  ...

}

# ATOMIC?

Thanks!

chema989
  • 3,962
  • 2
  • 20
  • 33
Balan Narcis
  • 139
  • 1
  • 10

1 Answers1

9

According to the C++ standard, potentially-concurrent access happens when the same variable is used from multiple threads, and these accesses conflict if at least one access is a write.

Potentially-concurrent accesses that conflict constitutes a data race, which is undefined behavior unless all such accesses are atomic. volatile will not save you.

Primitive types are not atomic within the meaning used in the C++ standard. You can use the std::atomic template to make objects which are atomic.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720