C ++控制台输入块,因此我无法杀死线程 [英] C++ console input blocks so i can't kill thread

查看:79
本文介绍了C ++控制台输入块,因此我无法杀死线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序有许多不同的线程处理不同的事情,其中​​之一处理用户输入.

My program has many different threads handling different things, and one of them deals with user input.

其他线程没有太多阻止调用的方式,那些阻止调用的线程是基于网络的,因此在套接字关闭时将被中断或正常返回.

The other threads don't have much in the way of blocking calls, and those that do block are network based so will be interrupted or return gracefully when the socket is shut down.

但是,用户线程已调用std::cin以便获取用户输入.这样做的结果是,当所有其他线程都死掉时,用户线程仍然在用户输入上受阻,并且只会在下一次给出输入时消失.

However the user thread has calls to std::cin in order to grab the user input. The effect this has is while all the other threads are dead the user thread is still blocking on user input, and will only die the next time input is given.

我有什么办法可以在阻止之前检查是否有任何用户输入要抓取?

Is there any way for me to check if there is any user input to grab before blocking?

我了解cin.peek()存在,但是根据我的经验,如果没有任何内容可以读取,它会阻塞.假定我正确使用了它

I understand cin.peek() exists but from my experience, it blocks if there is nothing to read in. Assuming I'm using it correctly

我的代码基本上是一个无限循环,当另一个线程切换条件变量时会停止:

My code is basically an infinite loop that stops when another thread switches the condition variable:

void doLoop()
{
    while (running) //running is shared between all threads and all others die quickly when it is false. It's set to true before the threads are started
    {
        string input = "";
        getline(cin, input);

        //Handle Input

    }
}

我在Windows上使用VS2013,无法使用外部库.我在整个过程中都使用windows.h和std.

I'm on windows, using VS2013, and cannot use external libraries. I'm using windows.h and std throughout.

推荐答案

您可以使用期货来允许用户输入带有时间限制的内容.然后,您可以将此代码插入主循环

What you could do is using futures to allow the user to input something with a time limit. You can then insert this code into your main loop

#include <iostream>       // std::cout
#include <future>         // std::async, std::future
#include <chrono>         // std::chrono::milliseconds
#include <string>
using namespace std;


bool myAsyncGetline(string & result)
{
    std::cout<<"Enter something within the time limit"<<endl;
    getline(cin,result);
    return true;
}

int main()
{
  // call function asynchronously:
  string res;
  std::future<bool> fut = std::async (myAsyncGetline,res); 


  std::chrono::seconds span (20);
  if (fut.wait_for(span)==std::future_status::timeout)
    std::cout << "Too Late!";
  else
      cout<<"You entered "<<res<<" "<< endl;

  return 0;
}

此功能在VS2012中可用,因此您应该能够复制它.

This is available in VS2012 so you should be able to reproduce it.

输出为"Tool Late!".如果getline在超时(20秒)后仍在工作,则输出结果.

The output is "Tool Late!" if getline is still working after the timeout (20s), otherwise it outputs the result.

我认为这比弄死线程更简单,因为如果达到时间限制,函数会自行停止. 告诉我是否需要帮助,可以将其集成到您现有的代码中.

I think that it is simpler than messing around with killing thread as the function stop by itself if the time limit is hit. Tell me if you need help integrating it into your existing code I can assist.

这篇关于C ++控制台输入块,因此我无法杀死线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆