...
Another approach is to embed a concurrently accessed object inside a union alongside a long
object or other padding to ensure that the object is the only one accessed at that address. This technique effectively guarantee that no two object are accessed simultaneously.
Compliant Code Example (C11)
In this compliant code example, two threads simultaneously modify two distinct members of a structure:
Code Block | ||||
---|---|---|---|---|
| ||||
struct multi_threaded_flags {
unsigned char flag1;
unsigned char flag2;
};
struct multi_threaded_flags flags;
int thread1(void *arg) {
flags.flag1 = 1;
return 0;
}
int thread2(void *arg) {
flags.flag2 = 2;
return 0;
} |
Unlike C99, C11 explicitly defines a memory location and provides the following note in subclause 3.14.2 [ISO/IEC 9899:2011]:
NOTE 1 Two threads of execution can update and access separate memory locations without interfering with each other.
In a C99 or earlier compliant compiler it is possible that flag1
and flag2
are stored in the same word. If both assignments occur on a thread-scheduling interleaving that ends with both stores occurring after one another, it is possible that only one of the flags will be set as intended, and the other flag will equal its previous value, because both chars are represented by the same word, which is the smallest unit the processor can work on. Before the changed made to the C Standard for C11, the Standard made no guarantees that these flags can be modified concurrently.
Even though each thread is modifying a separate object, they may be modifying the same word in memory. A similar problem is discussed in CON00-C. Avoid race conditions with multiple threads, but this example can be harder to diagnose because it is not immediately obvious that the same memory location is being modified.
Noncompliant Code Example (Bit-field)
...
Static assertions are discussed in detail in DCL03-C. Use a static assertion to test the value of a constant expression.
Compliant Code Example (C11)
In this compliant code example, two threads simultaneously modify two distinct members of a structure:
Code Block | ||||
---|---|---|---|---|
| ||||
struct multi_threaded_flags {
unsigned char flag1;
unsigned char flag2;
};
struct multi_threaded_flags flags;
int thread1(void *arg) {
flags.flag1 = 1;
return 0;
}
int thread2(void *arg) {
flags.flag2 = 2;
return 0;
} |
Unlike C99, C11 explicitly defines a memory location and provides the following note in subclause 3.14.2 [ISO/IEC 9899:2011]:
NOTE 1 Two threads of execution can update and access separate memory locations without interfering with each other.
In a C99 or earlier compliant compiler it is possible that flag1
and flag2
are stored in the same word. If both assignments occur on a thread-scheduling interleaving that ends with both stores occurring after one another, it is possible that only one of the flags will be set as intended, and the other flag will equal its previous value, because both chars are represented by the same word, which is the smallest unit the processor can work on. Before the changed made to the C Standard for C11, the Standard made no guarantees that these flags can be modified concurrently.
Even though each thread is modifying a separate object, they may be modifying the same word in memory. A similar problem is discussed in CON00-C. Avoid race conditions with multiple threads, but this example can be harder to diagnose because it is not immediately obvious that the same memory location is being modified.
Risk Assessment
Although the race window is narrow, having an assignment or an expression evaluate improperly because of misinterpreted data can result in a corrupted running state or unintended information disclosure.
...