1[/
2 / Copyright (c) 2003-2021 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
8[section:signals Signal Handling]
9
10Boost.Asio supports signal handling using a class called [link
11boost_asio.reference.signal_set signal_set]. Programs may add one or more signals to
12the set, and then perform an `async_wait()` operation. The specified handler
13will be called when one of the signals occurs. The same signal number may be
14registered with multiple [link boost_asio.reference.signal_set signal_set] objects,
15however the signal number must be used only with Boost.Asio.
16
17  void handler(
18      const boost::system::error_code& error,
19      int signal_number)
20  {
21    if (!error)
22    {
23      // A signal occurred.
24    }
25  }
26
27  ...
28
29  // Construct a signal set registered for process termination.
30  boost::asio::signal_set signals(io_context, SIGINT, SIGTERM);
31
32  // Start an asynchronous wait for one of the signals to occur.
33  signals.async_wait(handler);
34
35Signal handling also works on Windows, as the Microsoft Visual C++ runtime
36library maps console events like Ctrl+C to the equivalent signal.
37
38[heading See Also]
39
40[link boost_asio.reference.signal_set signal_set],
41[link boost_asio.examples.cpp03_examples.http_server HTTP server example (C++03)],
42[link boost_asio.examples.cpp11_examples.http_server HTTP server example (C++11)].
43
44[endsect]
45