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,563
  • 2
  • 18
  • 32
Balan Narcis
  • 127
  • 1
  • 9

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
  • 269,602
  • 39
  • 394
  • 697