Created
July 28, 2021 12:21
-
-
Save mrbald/ab46aa1afec1d64e1849718678b12db7 to your computer and use it in GitHub Desktop.
ASIO-based main.cpp skeleton
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/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