Created
May 19, 2013 05:52
-
-
Save usagi/5606828 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 <stdexcept> | |
#include <thread> | |
#include <future> | |
#include <iostream> | |
#include <boost/asio.hpp> | |
int main() | |
try | |
{ | |
auto future = std::async | |
( std::launch::async | |
, [&] | |
{ | |
try | |
{ | |
throw std::runtime_error("answer is 42."); | |
} | |
catch(...) | |
{ | |
std::cerr << "exception on an async thread, raise SIGINT.\n"; | |
raise(SIGINT); | |
std::rethrow_exception(std::current_exception()); | |
} | |
} | |
); | |
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 << ")"; | |
} | |
std::cerr << "\n"; | |
} | |
else | |
std::cerr << "error_code is " << e << "\n"; | |
} | |
); | |
aios.run(); | |
future.get(); | |
} | |
catch (const std::exception& e) | |
{ | |
std::cerr << "caught an std::exception, what: " << e.what() << "\n"; | |
return -1; | |
} | |
catch (...) | |
{ | |
std::cerr << "caught unknown exception.\n"; | |
return -2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
run result:
exception on an async thread, raise SIGINT.
SIGINT
caught an std::exception, what: answer is 42.