相关疑难解决方法(0)

使用std :: cin语句进行自动超时

我写了这个程序

#include<iostream>
using namespace std;
int n;
int main(int argc, char *argv[])
{
  std::cout << "Before reading from cin" << std::endl;

   // Below reading from cin should be executed within stipulated time
   bool b=std::cin >> n;
   if (b)
      std::cout << "input is integer for n and it's correct" << std::endl;
   else
      std::cout << "Either n is not integer or no input for n" << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这里std :: cin语句将等待控制台输入并进入睡眠模式,直到我们提供一些输入并按Enter键.

我希望std :: cin语句在10秒后超时(如果用户在10秒之间没有输入任何数据,那么编译器将开始执行std :: cin语句下面的程序的下一个语句.

我能够使用多线程机制解决它.以下是我的代码:

#include<unistd.h>
#include<stdlib.h>
#include<pthread.h> …
Run Code Online (Sandbox Code Playgroud)

c++ linux multithreading

2
推荐指数
1
解决办法
1544
查看次数

标签 统计

c++ ×1

linux ×1

multithreading ×1