...
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 noncompliant 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 Although this code appears to be harmless, 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. C99 makes 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.
Compliant Solution (C11)
The same code is compliant when run on a C11-compliant platform. Unlike C99, C11 explicitly defines a memory location and provides the following note in subclause 3.14.2 [ISO/IEC 9899:2011]:
...
.
Noncompliant Code Example (Bit-field)
Adjacent bit-fields may be stored in a single memory location. Consequently, modifying adjacent bit-fields in different threads is undefined behavior:
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <threads.h>
#include <assert.h>
struct multi_threaded_flags {
unsigned int flag1 : 2;
unsigned int flag2 : 2;
};
union mtf_protect {
struct multi_threaded_flags s;
long padding;
};
struct mtf_mutex {
union mtf_protect u;
mtx_t mutex;
};
struct mtf_mutex flags;
void chk_flags(void) {
static_assert(sizeof(long) >=
sizeof(struct multi_threaded_flags),
"A long type will not hold the flags on this architecture.");
}
int thread1(void *arg) {
if (thrd_success != mtx_lock(&flags.mutex)) {
/* Handle error */
}
flags.u.s.flag1 = 1;
if (thrd_success != mtx_unlock(&flags.mutex)) {
/* Handle error */
}
return 0;
}
int thread2(void *arg) {
if (thrd_success != mtx_lock(&flags.mutex)) {
/* Handle error */
}
flags.u.s.flag2 = 2;
if (thrd_success != mtx_unlock(&flags.mutex)) {
/* Handle error */
}
return 0;
}
|
...