Created
October 14, 2012 01:56
-
-
Save HappyCerberus/3886984 to your computer and use it in GitHub Desktop.
C++11 vs. volatile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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