I have an array of structs that's a global variable and one of the struct members is a boolean. One thread in a particular CPU is reading the boolean value, and another thread in a different CPU can periodically change the boolean value by assigning it true or false (my_array[index].bool = true/false
).
I understand that not using any form of synchronization mechanism will lead to undefined behavior.
However, assuming that:
- in my implementation,
sizeof(bool)==1
, - and from this previous post, setting a one byte boolean variable to true/false will be an atomic operation on an x86_64 bit system,
- and this answer paragraph 3 suggests that the MESIF protocol will keep the caches of multiple cores coherent (including the L1 private caches of different cores)
- my code will only ever run on an 64-bit x86 architecture with gcc 4.8.4 compiler optimization flag O3
Can I say with certainty that under the above-mentioned conditions, I can get away without using synchronization mechanisms like mutex locks, etc and not have undefined behavior?