Waiting for unput while printing output

Started by
6 comments, last by alvaro 15 years, 2 months ago
Lets say I have a console window open. It is waiting for input that can be typed in at any time. Is there a way to print things to that console while still waiting for input?
Advertisement
Most likely by default your program has a single thread.
Do you know how to create another thread (with that language you didn't mention)?

Create another thread. Have it start printing when you begin to wait for input. Stop that thread when input is received.

Or wait for input in an active fashion like:
while (!IsInputReceived())    PrintSomething();

(in other words, use non blocking input polling)
Sorry, it's C++. I assumed I had to create a new thread for my console so input wouldn't freeze my game. But then this problem comes up. I'd like to just let std::cin sit there and wait for input while other things went on printing. How would I stop the output thread when input is received? Also, I've only been briefly taught about threads. I can set it up I think, but is the thread function suppose to be called every loop? Or should the thread have a loop within itself and not return until its done at the end of the applications run time.
Here's a small sample, create a new console application and paste this code into the .cpp file
#include <windows.h>#include <iostream>HANDLE inputReceivedEvent = NULL;DWORD WINAPI ThreadProc(void*){	// Keep outputing as long as no input is received	// Wait aproximately 1 second each time	while (WaitForSingleObject(inputReceivedEvent, 1000) == WAIT_TIMEOUT)	{		std::cout << "Waiting for input" << std::endl;			}	return 0;}int _tmain(int argc, _TCHAR* argv[]){	// Create an event object. It will be used to signal the other thread that input has been received	inputReceivedEvent = CreateEvent(NULL, false, false, NULL);	// Create and run the output thread	HANDLE h = CreateThread(NULL, 0, ThreadProc, 0, 0, NULL);	// Wait for som input	int i;	std::cin >> i;	// At this point, input is received, so we set the event	SetEvent(inputReceivedEvent);	// We wait for the output thread to be really terminated. (this is optional)	WaitForSingleObject(h, INFINITE);	std::cout << "Input received, output thread terminated." << std::endl;	return 0;}
Thanks! I still have a couple question about threads though. If the thread doesn't have a loop inside it, does it need to be called every frame? Is it good to start a new thread every frame?
No it's not good. Create a thread for one job and keep it running until the job is done.

Most engines even go a step further and don't destroy a thread even when it's job is done. Instead, they have the thread sit idle until they feed it another job (this mechanism is often referred as a thread pool)
So I have the console open. I have a thread taking input and the main thread printing something every second or so. The problem now is that if I'm typing and the main thread wants to print something, it will print right in the middle of what I'm typing. Do I have to set up some console buffers to keep 1 line clean for input?
As an alternative to having two threads, you can query the input stream frequently to see if there is anything available to process. In any case, you'll still have the same problem with the output being intermingled with what the user is typing.

The solution to that problem could be having a lot more control on how the input is processed than simply using `std::cin >> my_line;', so you can delete what was typed so far, print some output and then display back the partial input.

By now your code will not be platform independent, so perhaps it's easier to make a GUI with two widgets at this point.

This topic is closed to new replies.

Advertisement