Consider I have a struct:
struct SimpleStruct {
int x;
int y;
int z;
}
Now if I have 3 separate threads, each which only updates one of the x,y,z fields of the struct respectively, is it safe to let them update concurrently, or should I use a mutex or something to stop that from happening?
It is safe (structs are aligned).
However you should be careful about false sharing (see Herb Sutter's article about it): if the fields are in the same cache line the writes will be effectively serialized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With