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

后端 未结 2 1968
后悔当初
后悔当初 2021-02-06 16:54

I want to write a few extra lines to a file when interrupted with ctrl-c before the program dies. However the location of the file is not hard coded so I need something more tha

2条回答
  •  故里飘歌
    2021-02-06 17:34

    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
    

提交回复
热议问题