说我有
bool unsafeBool = false;
int main()
{
std::thread reader = std::thread([](){
std::this_thread::sleep_for(1ns);
if(unsafeBool)
std::cout << "unsafe bool is true" << std::endl;
});
std::thread writer = std::thread([](){
unsafeBool = true;
});
reader.join();
writer.join();
}
保证作者写完后就unsafeBool
变成了。true
我知道阅读器输出的是未定义的行为,但据我所知,写入应该没问题。