UNDER CONSTRUCTION
When accessing a bit-field, a thread may inadvertently access a separate bit-field in adjacent memory. This is because compilers are required to store multiple adjacent bit-fields in one storage unit whenever they fit. Consequently, data races may exist not just on a bit-field accessed by multiple threads but also on other bit-fields sharing the same byte or word. The problem is difficult to diagnose because it may not be obvious that the same memory location is being modified by multiple threads.
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <mutex> struct multi_threaded_flags { unsigned int flag1 : 2; unsigned int flag2 : 2; }; struct mtf_mutex { struct multi_threaded_flags s; std::mutex mutex; }; struct mtf_mutex flags; int thread1(void *arg) { std::lock_guard<std::mutex> guard(&flags.mutex); flags.s.flag1 = 1; return 0; } int thread2(void *arg) { std::lock_guard<std::mutex> guard(&flags.mutex); flags.s.flag2 = 2; return 0; } |
...