kbhit() and threads
I am having an issue using the kbhit() and getch() functions when they are called in a thread. To give some background, I am trying to have a program that asks for a users input, but only gives them a certain amount of time to respond. To accomplish this, I made a thread that asks for the user input by doing the following:
Basically the thread is spawned from main, where I then call a Sleep function for an x amount of time. Once the time has passed I set the flag input_time_up = to 1 which terminates the thread.
input[i] = getch();
if (input_time_up == 1)
i = 0;
The problem that is occuring is that this thread is repeatedly created and destroyed inside a while loop in main (its used in a game where the user has a limited amount of time to retype a string). The first time the thread is created, kbhit() works just fine and the user input gets correctly put into the array. The second time the thread is created however, kbhit does not recognize the keys the user hit until after the thread termiates, where upon the user input starts backing up in the keyboard and input buffers casuing all kinds of grief. I've tried flushing stdin at all points of execution, tried rewind() to flush the keyboard buffer, tried to lock stdin inside the thread using flockfile() but for some reason its returns an error saying flockfile() is undeclared even though ive included stdio.h. I'm wondering if there is some special caveat with kbhit() and threads that I don't know about? I've searched around on google for a few hours and nothing leads me to believe there is.
Any help on this matter, with either kbhit() or an alternative solution to the problem of timed input would be greatly appreciated.
The documentation explicitly states that kbhit() shouldn't be used with Win32 apps and GUI apps. The reason is obvious: in Win32 apps you don't have a single source of imput but multiple sources (each window, dialog box etc.). So you need to look for an alternative technique.
Does anyone know how to do this in a console application? I'm a beginner in programming and I need a thing where it gives the user a certain amount of time to input a variable and if it runs out of time outputs a message saying the user lost. If anyeone knows how to do this and could tell me I'd be very grateful.
You can use a signal or a timer, but stick to one thread and use multiprocessing instead. Multiporcessing means using fork() or spwan() or exec(), depending on your OS and compiler. If the program is waiting for input, and the user doesn't enter any data within a limited amount of time, the timer will expire, causing a signal to be raised. The program should define a handler for such a signal and launch another process, while killing the current one (i.e., killing itself, after issuing an error message).
Uhm...could you post the code for that or explain it simply? Because I didn't understand 90 percent of what you just said. lol. I'm very new to c++ but I need to figure out how to do this.
EDIT: I understood the idea of it, but I didn't understand the thread things or how to do it.
Well, it's a bit lengthy and prtetty much platform and OS dependent code. However, if signals are your main concern, you can read about them here:
you can find more documentation here:
As for multiprocessing: you simply have to create a new process from the parent process, and let the parent kill itself. Each OS does it differently.
ok, thanks. I have one other question. How do you make a function run att the same time as the main? I'm not sure, it looked like the signal thing covered that a little but I didn't quite understand it. I want to have function A run at the same time as main. but I'm not quite sure how to split the path and have both run at the same time. You know instead of going main, a, main.
Have it go main, a and main, main. So...yea.... if someone could help me that would be great.
BTW I'm running windows, with usually metrowerks codewarrior compiler or sometimes dev-c++ compiler.
Last edited by pyrorocker93; 03-22-2006 at 10:27 AM.
The point of a thread is that it runs "at the same time" as other threads (main is the "main thread" so to speak). This of course ignores the low level detail that on a single processor machine, only one thing can "run" at a one instant in time -- the OS swaps between the threads and programs (hopefully, so fast that you can't tell it from the user side). (modern hardware is complex, but this is still "mostly" true).
So by putting "a" in a thread (this is not C++ but a platform library, so each compiler and OS do it a little differently) you will have a and Main running at the same time (from a user perspective).
In visual studio, a simple thread can be created with afxBeginThread(function*, void*, flags) where the void * is the function*'s parameters (more or less, I always have to look up the details here so Im sure its not 100% correct, but you get the idea). I have no idea what your compilers use instead, but if you look up "thread" in the help there is probably an example of it.
The gotcha of threaded programs:
If main needs a value from "a" and "a" needs something from main, you can get into a deadlock where the processes both wait on each other. You must take care to write code that does not have this problem.
Also, static variables in a thread function that is spawned more than once can really create chaos. Ditto for globals or shared memory of any kind. Investigate the volatile keyword (c++ standard).
Hello, i am new to this forum. First, thanks for any help
I read the help file, but i still don't quite understand how should i get into my program. Can anyone post here just a simple sample which would handle this kind of interrupts, i would appreciate it.
Yea, could you do a quick example of a program that uses threads? or more speficly a program that makes it where if the user doesn't input an integer within a certain amount of time it run's a code ending the program? :-p I've gotta figure this out one way or another. Also to make a code for if the variable doesn't have a value would you just do:
variableA == void
? or is there another complex and difficult to understand way to do that too. Thanks for all you guys help. Sorry I'm so new at this.
Last edited by pyrorocker93; 03-23-2006 at 12:01 AM.
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL