This would be done without mutexes. I just want to know if it is thread safe ie the program will not crash or something similar. I do not really care that the variable could be in the wrong state when it is read as the consequence is not fatal (only impact would be making a copy of a small data structure when not necessary)
I am using Qt.
Here is the context that could happen: from the main thread, I would be setting a bool that is a member of a qthread. While or after I am setting the bool to true, the qthread could be in the function that uses this bool (this is a function that is used by the main thread and the qthread, everything is already protected and synchronised so as to not have the function being run by both threads simultaneously)
Kinda think of it, since this bool is only used by the main thread anyways I will check which thread is in the function and that should solve that but I am still curious.
You seem to be asking about what are called benign races.
For example consider an integer variable representing a progress value in the range 0 to 100. You read it from one thread at the same time as you are incrementing from another. You don’t mind whether you read the value before the write or the value after the write. You’ll read it again soon and then you’ll get the value from after the write.
In that scenario the race is benign. However what may not be benign is something called tearing. The hardware may allow you to read the value whilst only some part of the memory has been written to by the other thread.
For example the writing thread may write the low word, then the read of both words occurs and then the high word is written. Tearing can lead to incorrectness.
The hardware does provide guarantees about tearing but they vary between architectures. For example x86 has atomic access, i.e. no tearing, for 4 byte wide data that is 4 byte aligned.
If you are writing portable code then you are in quite tricky waters because the current standard gives no guarantees about atomicity. If you can make assumptions on your hardware platform then you may be able to take advantage of benign races.