Skip to content

Instantly share code, notes, and snippets.

@mrbald
Created July 28, 2021 12:21
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 mrbald/ab46aa1afec1d64e1849718678b12db7 to your computer and use it in GitHub Desktop.
Save mrbald/ab46aa1afec1d64e1849718678b12db7 to your computer and use it in GitHub Desktop.
ASIO-based main.cpp skeleton
#include <boost/asio/io_context.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/signal_set.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include "logger.hpp"
namespace {
inline int run_io_loop(asio::io_context& ctx, asio::signal_set& signals) {
signals.async_wait([&](auto const& ec, int signal_number) {
if (!ec) {
LOG_INFO("terminal signal [{}] received, terminating", signal_number);
ctx.stop();
}
});
try {
while (ctx.poll() || ctx.run_one())
;
} catch (...) {
LOG_CRIT("exception: {}", boost::current_exception_diagnostic_information());
logger->flush();
return 1;
}
LOG_INFO("bye");
logger->flush();
return 0;
}
} // namespace
int main(int argc, char** argv) {
auto ctx = asio::io_context{1};
auto signals = asio::signal_set{ctx, SIGINT /*, SIGTERM*/};
post(ctx, [&] {
// code to start the application goes here
});
int const rc = base::run_io_loop(rctx, signals);
// perform cleanup, terminate and join background threads, etc.
return rc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment