简体   繁体   English

等待指令而不停止while循环

[英]Wait for instruction without stopping while loop

I designed a C++ application that represents an mechanical arm. 我设计了一个代表机械臂的C ++应用程序。 The simulation is running inside a while loop. 仿真在while循环内运行。 I have the following class Motherboard that interacts with the simulation 我有以下与仿真互动的类主板

class Motherboard
{
public:
void receiveInstruction(double angle);
}

I have as well an event receiver that waits for keyboard instruction during the simulation. 我还有一个事件接收器,它在模拟过程中等待键盘指令。 I won't copy the full class but here is the interesting part (the prefix irr comes from the fact that I am using Irrlicht 3D engine) : 我不会复制完整的类,但这是有趣的部分(前缀irr来自我正在使用Irrlicht 3D引擎的事实):

case irr::KEY_SPACE:
     // Do something
     return true;
case irr::KEY_RETURN:
     std::cin << angle
     motherboard->receiveInstruction(atof(angle))
     return true;

where angle is of type char[] and atof converts a string into a double. 其中angle为char []类型,atof将字符串转换为双精度型。 When I enter the RETURN key on my keyboard the simulation freezes until i complete typing the angle instruction. 当我在键盘上输入RETURN键时,模拟冻结,直到我完成角度指令的输入。

How shall I proceed to let the simulation running while I am typing the instruction, and send the instruction as soon as I am done entering the information ? 我在输入指令时如何继续运行模拟,并在输入信息后立即发送指令? Are threads unavoidable ? 线程是不可避免的吗? (I am running on Windows) (我在Windows上运行)

Best regards 最好的祝福

Vincent 文森特

是的,似乎是不可避免的,或者至少是最优雅的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM