kbhit() and threads


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: kbhit() and threads

  1. #1
    Join Date
    Mar 2006
    Posts
    1

    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:

    HTML Code:
    while(1)
      {
             if (kbhit())
             {
                         input[i] = getch();
                         i++;
             }
             if (input_time_up == 1) 
             {
                  i = 0;             
                  _endthread();
             }
      }
    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.

    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.

    Thanks!
    Billy

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    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.
    Danny Kalev

  3. #3
    Join Date
    Mar 2006
    Posts
    5
    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.

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    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).
    Danny Kalev

  5. #5
    Join Date
    Mar 2006
    Posts
    5
    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.

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    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:
    http://www.devx.com/DevX/LegacyLink/9404
    you can find more documentation here:
    http://www.informit.com/guides/conte...lus&seqNum=125
    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.
    Danny Kalev

  7. #7
    Join Date
    Mar 2006
    Posts
    5
    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 09:27 AM.

  8. #8
    Join Date
    Dec 2003
    Posts
    3,366
    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).

  9. #9
    Join Date
    Mar 2006
    Posts
    1
    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.

  10. #10
    Join Date
    Mar 2006
    Posts
    5
    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-22-2006 at 11:01 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center