Assuming I have an array like this in C:
static volatile bool my_array[128] = {0};
And on top of that I have 128 threads and each thread writes to a different index true whenever it is finished.
Can this produce some issues? Writing a single byte has no impact to the surrounding bytes on x64, right?
I'm just asking, because I'm not sure if the CPU / memory controller / etc. always reads 8 (or 4) bytes and then writes them. Which would create some weird race condition.
Thanks a lot
No two elements of the array are stored in the same memory location as defined by the C standard, so concurrent access or modification of separate elements of the array do not interfere with each other, as confirmed by C17 3.14 paragraph 2:
The only thing to be careful of is the concurrent update of different bit-field members in a sequence of adjacent bit-fields all having nonzero width, as confirmed by the next paragraph of C17 3.14:
(emphasis mine).
The next paragraph of C17 3.14 provides an example:
NOTE
I speculate that concurrent access to different elements of a "packed" array, or to different members of a packed structure is potentially unsafe on some architectures, but that is outside the scope of the C specification.