Lets say I havve an array of two structs of the type mystruct_t:
typedef struct {
uint16_t member1;
bool member2;
} mystruct_t;
mystruct_t my_array[2];
What would happen if two tasks tried to access different parts of this array at the same time? If one task tried to access my_array[0] and another task tried to access my_array[1], would this create a race condition?
What would happen if two tasks tried to access different parts of the struct at the same time? If one task tried to access my_array[0].member1 and another task tried to access my_array[0].member2, would this create a race condition?
Update: I'm using version c99.
A race condition means the two tasks (threads) would get different behavior depending on ordering. This only comes into play if one or both tasks are writing (opposed to just reading) to the same location (C11). Per section 3.14 memory location:
NOTE 1 Two threads of execution can update and access separate memory locations without interfering with each other.
NOTE 2 [...]
EXAMPLE A structure declared as
struct { char a; int b:5, c:11, :0, d:8; struct { int ee:8; } e; }
contains four separate memory locations: The member a, and bit-fields d and e.ee are each separate memory locations, and can be modified concurrently without interfering with each other. The bit-fields b and c together constitute the fourth memory location. The bit-fields b and c cannot be concurrently modified, but b and a, for example, can be.
For reading (as well as writing), you may still get performance impacts due to cache coherence.
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