これは、 C++11のVolatileのフォローアップです。
質問では、次のコードがC ++ 11で未定義の動作を示していると言われ、非常に混乱しました。C ++ 11でのvolatileの動作に関して、私が読むことができる資料(おそらく標準のセクション)はありますか?
または、誰かが問題の場所を説明できますか?
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
volatile int notify;
void watcher()
{
this_thread::sleep_for(chrono::seconds(2));
notify = 1;
cout << "Notification sent." << endl;
}
int main()
{
thread(watcher).detach();
notify = 0;
while (!notify)
{
cout << "Waiting." << endl;
this_thread::sleep_for(chrono::seconds(1));
}
cout << "Notification received." << endl;
return 0;
}