[关闭]
@john-lee 2021-01-03T13:40:59.000000Z 字数 745 阅读 618

信号处理(Signal Handling)

Boost.Asio


Boost1.Asio 支持使用名为 signal_set 的类进行信号处理。程序可以向集合中添加一个或多个信号,然后执行async_wait()操作。当其中一个信号出现时,将调用指定的处理程序。同一个信号可以注册到多个 signal_set 对象中,但是该信号只能用于 Boost.Asio。

  1. void handler(
  2. const boost::system::error_code& error,
  3. int signal_number)
  4. {
  5. if (!error)
  6. {
  7. // A signal occurred.
  8. }
  9. }
  10. ...
  11. // Construct a signal set registered for process termination.
  12. boost::asio::signal_set signals(io_context, SIGINT, SIGTERM);
  13. // Start an asynchronous wait for one of the signals to occur.
  14. signals.async_wait(handler);

信号处理也适用于Windows,因为 Microsoft Visual C++ 运行库将像 Ctrl+C 这样的控制台事件映射到等效信号。

另请参阅

signal_set, HTTP 服务器示例(C++03), HTTP 服务器示例(C++11)。


Copyright © 2003-2020 Christopher M. Kohlhoff

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注