Threads read/writing at the same time
c++, multithreading
Solution
According to the rules of the C++11 memory model:
- Thread1 and Thread2 read Flag1 at the exact same time? This is always safe.
- Thread1 is writing to Flag1 at the same times as Thread2 is reading flag1? This is a data race.
A data race is undefined behaviour. Although it's unlikely to crash on any sane hardware, it's undefined behaviour, so anything could happen.
Problem
If I don't care whether `Thread1` changes `Flag1` at the same time `Thread2` changes `Flag1`, is there anything else to worry about besides logic errors? Will it cause a crash etc if: - `Thread1` and `Thread2` read `Flag1` at the exact same time? - `Thread1` is writing to `Flag1` at the same time as `Thread2` is reading `Flag1`? In these examples, `Flag1` is a `bool`.