crt+c 后继续执行后面的代码(c++ SIGINT)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39675633/article/details/82263686

这里介绍使用c++ SIGNT的用法,即ctr+c后由操作系统发送一个中断信号给正在运行的进程,由中断函数选择其行为。这里仅仅给出SIGINT的例子,具体可参考这里

#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;
}

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

猜你喜欢

转载自blog.csdn.net/weixin_39675633/article/details/82263686
今日推荐