crt + c continues behind the code (c ++ SIGINT)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/weixin_39675633/article/details/82263686

Presented here using c ++ SIGNT usage, namely ctr + c is sent by the operating system after an interrupt signal to the running process, by the interrupt function to choose their behavior. There will be only examples of SIGINT specific reference herein .

#include <iostream>
#include <csignal>
bool flag = true;
void exit_while(int sig)
{
  flag = false;
}
int main()
{
  signal(SIGINT, exit_while);
  while (flag)
  {
    std::cout << "while" << std::endl;
  }
  std::cout << std::endl;
  int i = 10;
  while (i-- > 0)
    std::cout << "exit while" << std::endl;
  return 0;
}

Sample Output crt + c:

while
while
while^C

exit while
exit while
exit while
exit while
exit while
exit while
exit while
exit while
exit while
exit while

Guess you like

Origin blog.csdn.net/weixin_39675633/article/details/82263686