0

I designed a C++ application that represents an mechanical arm. The simulation is running inside a while loop. 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) :

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. When I enter the RETURN key on my keyboard the simulation freezes until i complete typing the angle instruction.

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)

Best regards

Vincent

Vincent
  • 1,616
  • 2
  • 16
  • 18

1 Answers1

0

Yeah it seems so, that a seperate thread is unavoidable or at least the most elegant way.

dgrat
  • 2,214
  • 4
  • 24
  • 46
  • Starting another thread from a key event handler to read the keyboard, doesn't seem all that elegant to me. – cHao Jun 11 '13 at 11:08
  • You should start the new thread from beginning and give information over shared memory or whatever. – dgrat Jun 11 '13 at 11:46
  • It appears there's already something watching the keyboard, though (going by the `case irr::KEY_SPACE` and `irr::KEY_RETURN`, which look an awful lot like key code constants). Another thread busybodying around with input could blow that code all to hell. – cHao Jun 11 '13 at 12:12
  • So you don't want to or cannot change the rest of the code? Maybe use a pointer or reference for the angle variable for the arm control and do the input in a seperate thread which writes direct into the memory of the angle variable. Then nothing stops and gets executed immediatly when when the value is changed. But you have to check the value in the loop continously. – dgrat Jun 11 '13 at 12:15
  • By all appearances, Irrlicht is using a Windows GUI-style (message passing / event driven) interface. You could *try* to handle input yourself directly, but something tells me it won't go all that well without sacrificing the ability to handle keyboard input the way a GUI app expects to. – cHao Jun 11 '13 at 12:25