Skip to content

Instantly share code, notes, and snippets.

@usagi
Created May 19, 2013 05:52
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 usagi/5606828 to your computer and use it in GitHub Desktop.
Save usagi/5606828 to your computer and use it in GitHub Desktop.
#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;
}
@usagi
Copy link
Author

usagi commented May 19, 2013

run result:

exception on an async thread, raise SIGINT.
SIGINT
caught an std::exception, what: answer is 42.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment