GCC 4.8还是对下面的问题做了修正
材料来源
有些机器(编译器)修改结构成员域是以word为单位的,如果word包括多个成员,又有多个thread对多个成员同时操作,就会发生race condition。
以下面的结构为例,IA-64修改full,是以8字节位单位(即把lock也读入写回)。假设两个线程如下场景:
T1 读入full
T2 修改lock
T1 修改full,写回(实际也把lock的原值写回),导致T2写入lock被破坏
struct btrfs_block_rsv {
u64 size;
u64 reserved;
struct btrfs_space_info *space_info;
spinlock_t lock;
unsigned int full:1;
};
令人恐怖的是
"C does not provide such guarantee, nor can you reliably lock different structure fields with different locks if they share
naturally aligned word-size memory regions. The C++11 memory model would guarantee this, but that's not implemented nor do you build
the kernel with a C++11 compiler."
阅读(724) | 评论(0) | 转发(0) |