1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Signal handler requirements</title> 5<link rel="stylesheet" href="../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../boost_asio.html" title="Boost.Asio"> 8<link rel="up" href="../reference.html" title="Reference"> 9<link rel="prev" href="ShutdownHandler.html" title="SSL shutdown handler requirements"> 10<link rel="next" href="SyncRandomAccessReadDevice.html" title="Buffer-oriented synchronous random-access read device requirements"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td> 15<td align="center"><a href="../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="ShutdownHandler.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="SyncRandomAccessReadDevice.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="boost_asio.reference.SignalHandler"></a><a class="link" href="SignalHandler.html" title="Signal handler requirements">Signal handler requirements</a> 28</h3></div></div></div> 29<p> 30 A signal handler must meet the requirements for a <a class="link" href="Handler.html" title="Handlers">handler</a>. 31 A value <code class="computeroutput">h</code> of a signal handler class should work correctly in 32 the expression <code class="computeroutput">h(ec, n)</code>, where <code class="computeroutput">ec</code> is an lvalue 33 of type <code class="computeroutput">const error_code</code> and <code class="computeroutput">n</code> is an lvalue of 34 type <code class="computeroutput">const int</code>. 35 </p> 36<h5> 37<a name="boost_asio.reference.SignalHandler.h0"></a> 38 <span class="phrase"><a name="boost_asio.reference.SignalHandler.examples"></a></span><a class="link" href="SignalHandler.html#boost_asio.reference.SignalHandler.examples">Examples</a> 39 </h5> 40<p> 41 A free function as a signal handler: 42 </p> 43<pre class="programlisting">void signal_handler( 44 const boost::system::error_code& ec, 45 int signal_number) 46{ 47 ... 48} 49</pre> 50<p> 51 A signal handler function object: 52 </p> 53<pre class="programlisting">struct signal_handler 54{ 55 ... 56 void operator()( 57 const boost::system::error_code& ec, 58 int signal_number) 59 { 60 ... 61 } 62 ... 63}; 64</pre> 65<p> 66 A lambda as a signal handler: 67 </p> 68<pre class="programlisting">my_signal_set.async_wait( 69 [](const boost::system::error_code& ec, 70 int signal_number) 71 { 72 ... 73 }); 74</pre> 75<p> 76 A non-static class member function adapted to a signal handler using <code class="computeroutput">std::bind()</code>: 77 </p> 78<pre class="programlisting">void my_class::signal_handler( 79 const boost::system::error_code& ec, 80 int signal_number) 81{ 82 ... 83} 84... 85my_signal_set.async_wait( 86 std::bind(&my_class::signal_handler, 87 this, std::placeholders::_1, 88 std::placeholders::_2)); 89</pre> 90<p> 91 A non-static class member function adapted to a signal handler using <code class="computeroutput">boost::bind()</code>: 92 </p> 93<pre class="programlisting">void my_class::signal_handler( 94 const boost::system::error_code& ec, 95 int signal_number) 96{ 97 ... 98} 99... 100my_signal_set.async_wait( 101 boost::bind(&my_class::signal_handler, 102 this, boost::asio::placeholders::error, 103 boost::asio::placeholders::signal_number)); 104</pre> 105</div> 106<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 107<td align="left"></td> 108<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 109 Kohlhoff<p> 110 Distributed under the Boost Software License, Version 1.0. (See accompanying 111 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 112 </p> 113</div></td> 114</tr></table> 115<hr> 116<div class="spirit-nav"> 117<a accesskey="p" href="ShutdownHandler.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="SyncRandomAccessReadDevice.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 118</div> 119</body> 120</html> 121