...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <threads.h> #include <assert.h> struct multi_threaded_flags { unsigned int flag1 : 2; unsigned int flag2 : 2; }; struct mtf_mutex { struct multi_threaded_flags s; mtx_t mutex; }; struct mtf_mutex flags; int thread1(void *arg) { if (thrd_success != mtx_lock(&flags.mutex)) { /* Handle error */ } flags.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.s.flag2 = 2; if (thrd_success != mtx_unlock(&flags.mutex)) { /* Handle error */ } return 0; } |
Static assertions are described in detail in DCL03-C. Use a static assertion to test the value of a constant expression.
Compliant Solution (C11)
In this compliant solution, two threads simultaneously modify two distinct members of a structure:
...