signals.qbk 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. [/
  2. / Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:signals Signal Handling]
  8. Boost.Asio supports signal handling using a class called [link
  9. boost_asio.reference.signal_set signal_set]. Programs may add one or more signals to
  10. the set, and then perform an `async_wait()` operation. The specified handler
  11. will be called when one of the signals occurs. The same signal number may be
  12. registered with multiple [link boost_asio.reference.signal_set signal_set] objects,
  13. however the signal number must be used only with Boost.Asio.
  14. void handler(
  15. const boost::system::error_code& error,
  16. int signal_number)
  17. {
  18. if (!error)
  19. {
  20. // A signal occurred.
  21. }
  22. }
  23. ...
  24. // Construct a signal set registered for process termination.
  25. boost::asio::signal_set signals(io_context, SIGINT, SIGTERM);
  26. // Start an asynchronous wait for one of the signals to occur.
  27. signals.async_wait(handler);
  28. Signal handling also works on Windows, as the Microsoft Visual C++ runtime
  29. library maps console events like Ctrl+C to the equivalent signal.
  30. [heading See Also]
  31. [link boost_asio.reference.signal_set signal_set],
  32. [link boost_asio.examples.cpp03_examples.http_server HTTP server example (C++03)],
  33. [link boost_asio.examples.cpp11_examples.http_server HTTP server example (C++11)].
  34. [endsect]