Skip to content

Instantly share code, notes, and snippets.

@HappyCerberus
Created October 14, 2012 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HappyCerberus/3886984 to your computer and use it in GitHub Desktop.
Save HappyCerberus/3886984 to your computer and use it in GitHub Desktop.
C++11 vs. volatile
#include <iostream>
#include <chrono>
#include <thread>
#include <atomic>
using namespace std;
// int notify;
// volatile int notify;
atomic<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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment