Boost logo

Boost Users :

Subject: [Boost-users] problem with asio signal_set and consecutive signals
From: Lutz Schneider (lutz.schneider_at_[hidden])
Date: 2014-08-27 16:11:23


I have a program and would like to stop it by sending SIGINT for writing
some data to a file instead of exiting immediately. However, if the user of
the program sends SIGINT again then the program should quit immediately and
forget about writing data to a file. For portability reason I would like to
use boost::asio for this purpose.

My initial (simplified) approach (see below) did not work. Is this not
possible or am I missing something?

handler seems to be called only once (printing out the message) and the
program always stops when the loop has reached the max iteration number.

void handler(
         const boost::system::error_code& error,
         int signal_number)
  if (!error)
      static bool first = true;
        std::cout << " A signal(SIGINT) occurred." << std::endl;
        // do something like writing data to a file
        first = false;
        std::cout << " A signal(SIGINT) occurred, exiting...." << std::endl;

int main(){
  // Construct a signal set registered for process termination.
  boost::asio::io_service io;
  boost::asio::signal_set signals(io, SIGINT);
  // Start an asynchronous wait for one of the signals to occur.
  size_t i;
    // time stepping loop, do some computations
  std::cout << i << std::endl;
  return 0;


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at