Closed Thread
Results 1 to 7 of 7

Thread: non blocking getchar.. possible?

  1. #1
    shannary is offline Newbie
    Join Date
    Apr 2007
    Posts
    4
    Rep Power
    0

    non blocking getchar.. possible?

    hi,
    does anyone knows if is there a way to make getchar non blocking?

    I'm using curses and I'm on linux platform. With a thread version of the same program I have solved my problems with getch and with the timeout function but now the program doesn't work properly and I must use
    Code:
    c=getchar();	
    if (c==0) c=getch();
    Using timeout(1) makes non blocking only the c=getch part of this code but not the getchar one.

    I have tried to solve this problem using this function
    Code:
    int kbhit(void)
    
    {
    
    struct timeval tv;
    
    fd_set read_fd;
    
    tv.tv_sec=0;
    
    tv.tv_usec=0;
    
    FD_ZERO(&read_fd);
    
    FD_SET(0,&read_fd);
    
    if(select(1, &read_fd, NULL, NULL, &tv) == -1)
    
    return 0;
    
    if(FD_ISSET(0,&read_fd))
    
    return 1;
    
    return 0;
    
    }
    and this code
    Code:
    if (kbhit()) 
    	 c = getchar();
    but the program doesn't work properly...

    Does anyone knows a solution?

    I'm 5 days in a row searching of solve this big problem unhappy

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    I don't understand what exactly you're trying to do, can you be more specific?
    I've no experience with curses or linux programming in general, so I'll probably be unable to help you.

  4. #3
    shannary is offline Newbie
    Join Date
    Apr 2007
    Posts
    4
    Rep Power
    0
    thx for the interest.
    I'm making a videogame like spaceinvaders and I want get input with getchar in way the getchar function doesn't block the program if no input by the user is pressed.. Curses provides two functions (getch and timeout) but they don't work in this case and I must use getchar..

    I hope I made myself clear

  5. #4
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    I think you need multithreading. And do you really have to use getchar()?
    getchar() expects you to press enter, before it "takes" the input - getch() doesn't, it "takes" exactly when you press it.

    In multithreading you're running two (or more) threads, with different tasks. In your project, a thread could have the task to get user input - without disturbing other threads (of course a bit, but the user will not see or feel it).

    I know enough of linux programming to know that you can use the library pthread, for multithreading. You can create a thread with pthread_create() and stop it using pthread_join(). The prototype for one of the arguments passed to pthread_create(), a function always have to be like this;
    Code:
    #include <pthread.h> // Just to show, what to include, if using the functions.
    // ...
    void *thread_function_name(void *argument_name);
    In the thread function, you can do something like this, to get the userinput, while in the game.
    Code:
    char c;
    while(1) // Forever
    {
        sleep(??); // Sleep for a little to prevent full CPU usage
    
        if(c = getch())
            printf("You typed, while in game: %c!\n", c); // What did the user type?
    }
    It's pretty primitive, but I hope you get the idea. If using getchar() the user have to press enter, as said before, before the input will get "accepted".

    I can't make any examples about it all, because I'm not experienced with Linux, but I hope it was enough to get you started.
    Last edited by v0id; 04-19-2007 at 11:20 AM.

  6. #5
    shannary is offline Newbie
    Join Date
    Apr 2007
    Posts
    4
    Rep Power
    0
    hi, unfortunately I can't use threads because my prof forbid it. I must use only multiprocessing and System V objects like shared memory and semaphores.

    I need to use getchar because getch isn't working properly with the use of System V objects.. It is very very strange
    thanks for the advices anyway

  7. #6
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    I'm afraid I can't help you then, sorry.
    Good luck! :-)

    (I'll be glad to hear about your solution, when the time comes)

  8. #7
    shannary is offline Newbie
    Join Date
    Apr 2007
    Posts
    4
    Rep Power
    0
    don't you worry, thanks anyway.

    Anyone that can help me?

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Getting around port blocking...
    By Chrisms in forum Computer Software/OS
    Replies: 13
    Last Post: 09-04-2010, 03:28 PM
  2. troubles with getchar
    By elvira23 in forum C and C++
    Replies: 16
    Last Post: 09-01-2009, 06:58 PM
  3. stl maps blocking?
    By manux in forum C and C++
    Replies: 7
    Last Post: 05-29-2009, 10:21 PM
  4. Blocking TV
    By AmbitionAtWork in forum General Programming
    Replies: 3
    Last Post: 07-17-2008, 09:58 PM
  5. Blocking GUI Movement escaping ur app
    By MXTECH in forum Visual Basic Tutorials
    Replies: 3
    Last Post: 07-02-2008, 08:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts