mer*_*011 3 c++ multithreading thread-safety
有许多相关的问题,但这个问题可以说更具体.
考虑以下示例程序,请注意,这bool只是一个普通的程序bool.
当我运行它时,它达到了预期的效果:用户按下后Enter,hello world将停止打印.
如果我只要求bar2() 最终开始返回后,立即run就是false,低于逻辑保证通过该标准是安全的?
#include <thread>
#include <mutex>
#include <unistd.h>
bool run = true;
void bar2() {
// Is this check safe without a lock?
if (!run) return;
printf("hello world\n");
fflush(stdout);
}
void bar() {
while (true) {
bar2();
sleep(1);
}
}
int main(){
std::thread(bar).detach();
getchar();
run = false;
// Prevent main from exiting.
getchar();
}
Run Code Online (Sandbox Code Playgroud)
标准不保证没有同步或排序保证,例如std::atomic其他线程将看到写入run.
事实上,编译器完全可以只检查一次是否write为真,并且因为线程本身从不写入它而不会重新加载它来缓存值.现在几乎可以说,编译器上的所有c库调用通常都不知道没有函数写入run和在x86下,你不必担心没有看到从其他处理器到内存的更新,所以它在实际工作中(和即使在其他架构下,上下文切换也可能解决问题).
但是,如果你纯粹从标准的角度谈论?没有任何保证.