asio C++ library

PrevUpHomeNext

Signal token requirements

A signal token is a completion token for completion signature void(error_code, int).

Examples

A free function as a signal token:

void signal_handler(
    const asio::error_code& ec,
    int signal_number)
{
  ...
}

A signal token function object:

struct signal_handler
{
  ...
  void operator()(
      const asio::error_code& ec,
      int signal_number)
  {
    ...
  }
  ...
};

A lambda as a signal token:

signal_set.async_wait(...,
    [](const asio::error_code& ec,
      int signal_number)
    {
      ...
    });

A non-static class member function adapted to a signal token using std::bind():

void my_class::signal_handler(
    const asio::error_code& ec,
    int signal_number)
{
  ...
}
...
signal_set.async_wait(...,
    std::bind(&my_class::signal_handler,
      this, std::placeholders::_1,
      std::placeholders::_2));

A non-static class member function adapted to a signal token using boost::bind():

void my_class::signal_handler(
    const asio::error_code& ec,
    int signal_number)
{
  ...
}
...
signal_set.async_wait(...,
    boost::bind(&my_class::signal_handler,
      this, asio::placeholders::error,
      asio::placeholders::signal_number));

Using use_future as a signal token:

std::future<int> f = signal_set.async_wait(..., asio::use_future);
...
try
{
  int signo = f.get();
  ...
}
catch (const system_error& e)
{
  ...
}

Using use_awaitable as a signal token:

asio::awaitable<void> my_coroutine()
{
  try
  {
    ...
    int signo =
      co_await signal_set.async_wait(
          ..., asio::use_awaitable);
    ...
  }
  catch (const system_error& e)
  {
    ...
  }
}

PrevUpHomeNext