Windows Ctrl-C - Cleaning up local stack objects in command line app

前端 未结 2 1217
礼貌的吻别
礼貌的吻别 2021-01-26 11:05

I\'m writing a console application Windows that creates some objects in the main thread and kicks those off into a loop, only exiting when the user uses the Ctrl-C interrupt. <

2条回答
  •  感情败类
    2021-01-26 11:36

    You make your loop on a condition variable.

    When you receive ctrl-C (SIGINT) you set the condition variable to false and return. The loop will then exit normally.

    bool finished = false;
    
    int main()
    {
          DBClient db;
          DataPuller p;
    
          while (!finished)
          {
               // ... do stuff until Ctrl-C comes in
          }
    }
    
    // Signal handler or Control handler on windows
    // Set finished = true.
    

提交回复
热议问题