Created
May 12, 2013 10:19
-
-
Save usagi/5563078 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <thread> | |
| #include <iostream> | |
| #include <boost/asio.hpp> | |
| int main() try | |
| { | |
| std::cout << "app start\n"; | |
| auto run_continues = true; | |
| auto main_task = [&] | |
| { | |
| do | |
| { | |
| std::this_thread::sleep_for | |
| ( std::chrono::seconds(1) ); | |
| std::cout << "tick " << std::flush; | |
| } | |
| while(run_continues); | |
| }; | |
| auto t = std::thread( main_task ); | |
| boost::asio::io_service aios; | |
| boost::asio::signal_set ss(aios, SIGINT, SIGTERM); | |
| ss.async_wait | |
| ( | |
| [&] | |
| ( const boost::system::error_code & e | |
| , int n | |
| ) | |
| { | |
| if(!e) | |
| { | |
| switch(n) | |
| { | |
| case SIGINT : std::cerr << "SIGINT"; break; | |
| case SIGTERM: std::cerr << "SIGTERM"; break; | |
| default : std::cerr << "SIG(" << n << ")"; | |
| } | |
| }else | |
| std::cerr << "error_code(" << e << ")"; | |
| } | |
| ); | |
| aios.run(); | |
| run_continues = false; | |
| t.join(); | |
| std::cout << "app end\n"; | |
| } | |
| catch(const std::exception& e) | |
| { | |
| std::cerr << "exception: " << e.what(); | |
| return -1; | |
| } | |
| catch(...) | |
| { | |
| std::cerr << "exception: (unkown exception parameter)"; | |
| return -2; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment