在不输入用户输入的情况下中断cin while循环 [英] Interrupt cin while loop without the user entering input
问题描述
在主
中,我使用户能够输入命令来停止应用程序:
In main
, I give the user the ability to enter a command to stop the application:
while( run_processes && cin >> command_line_input ){
我也想通过设置 run_processes = false;
来停止其他应用程序。
I'd also like to stop the application elsewhere by setting run_processes = false;
.
但是,何时我将 run_processes
设置为 false
,如果没有用户输入,上述循环就不会停止。
However, when I set run_processes
to false
, the above loop doesn't stop without the user entering input.
如何在没有用户输入的情况下正确中断循环?
How can I properly interrupt the loop without the user entering input?
推荐答案
不可能以可移植的方式中断 std :: cin
。
It is not possible to interrupt std::cin
in a portable way.
但是您可以解决非便携式解决方案,例如手动 poll()
在UNIX系统上的标准输入,并在轮询时检查 run_processes
。
You could however resolve to non-portable solution, e.g. manually poll()
the standard input on a UNIX system and check run_processes
while polling.
这篇关于在不输入用户输入的情况下中断cin while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!