In C++ when interrupted with ctrl-c call a function with arguments (other than signal number) before dying

前提是你 提交于 2019-12-03 08:59:23
dshepherd

Based on the answer to "How can I catch a ctrl-c event? (C++)" we can install a signal handler that simply raises an exception. Then we can catch the exception and run whatever standard c++ code we like.

Here's a working example:

#include <iostream>
#include <exception>
#include <signal.h>
#include <stdlib.h>


class InterruptException : public std::exception
{
public:
  InterruptException(int s) : S(s) {}
  int S;
};


void sig_to_exception(int s)
{
  throw InterruptException(s);
}


int main()
  {
    // Taken from answer to "How can I catch a ctrl-c event? (C++)"
    struct sigaction sigIntHandler;
    sigIntHandler.sa_handler = sig_to_exception;
    sigemptyset(&sigIntHandler.sa_mask);
    sigIntHandler.sa_flags = 0;
    sigaction(SIGINT, &sigIntHandler, NULL);


    try
      {
        std::cout << "Inside try" << std::endl;

        // Do something with output so the loop doesn't get optimised out
        double i = 0;
        while(i < 1e30) {i++;} // loop until interrupted
        std::cout << i << std::endl;
      }
    catch(InterruptException& e)
      {
        std::cout << "Write something to file" << std::endl;
        std::cout << "Caught signal " << e.S << std::endl;
        return 1;
      }

    return 0;
  }

This approach does have at least one downside: we have to wrap everything after the handler is installed in a try, otherwise interrupt signals would cause an abort().

One common scheme for loop-based scientific code is to have a global volatile sig_atomic_t boolean indicating whether a signal was caught and add it to the loop condition.

e.g.

volatile sig_atomic_t interrupted=false; 
...
void signal_handler(int s)
{
   // ...
   interrupted=true;
}
...
while (!converged && !interrupted)
{
     // Perform computations
}
// Save file properly
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!