大佬教程收集整理的这篇文章主要介绍了C控制台输入块所以我不能杀死线程,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
其他线程阻塞调用的方式并不多,而阻塞调用的线程基于网络,因此在套接字关闭时会被中断或正常返回.
但是,用户线程调用std :: cin以获取用户输入.这样做的结果是当所有其他线程都死了时,用户线程仍然阻塞用户输入,并且只会在下次输入时死亡.
有没有办法让我检查在阻止之前是否有任何用户输入要抓取?
我理解cin.peek()存在,但根据我的经验,如果没有什么可读的话它会阻塞.假设我正确使用它
我的代码基本上是一个无限循环,在另一个线程切换条件变量时停止:
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.
#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中可用,因此您应该能够重现它.
输出是“工具迟到!”如果getline在超时(20s)后仍然有效,否则输出结果.
我认为,如果时间限制被击中,它就比杀死线程更简单,因为函数会自动停止.告诉我,如果您需要帮助将其集成到我现有的代码中,我可以提供帮助.
以上是大佬教程为你收集整理的C控制台输入块所以我不能杀死线程全部内容,希望文章能够帮你解决C控制台输入块所以我不能杀死线程所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。