Created
August 4, 2016 17:03
-
-
Save najlepsiwebdesigner/86a992ea1700aa634657f840f3cfcafc to your computer and use it in GitHub Desktop.
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 <boost/thread.hpp> | |
#include <iostream> | |
using namespace std; | |
void ThreadFunction() | |
{ | |
int counter = 0; | |
for(;;) | |
{ | |
cout << "thread iteration " << ++counter << " Press Enter to stop" << endl; | |
try | |
{ | |
// Sleep and check for interrupt. | |
// To check for interrupt without sleep, | |
// use boost::this_thread::interruption_point() | |
// which also throws boost::thread_interrupted | |
boost::this_thread::sleep(boost::posix_time::milliseconds(500)); | |
} | |
catch(boost::thread_interrupted&) | |
{ | |
cout << "Thread is stopped" << endl; | |
return; | |
} | |
} | |
} | |
int main() | |
{ | |
// Start thread | |
boost::thread t(&ThreadFunction); | |
// Wait for Enter | |
char ch; | |
cin.get(ch); | |
// Ask thread to stop | |
t.interrupt(); | |
// Join - wait when thread actually exits | |
t.join(); | |
cout << "main: thread ended" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment