Thread: Using threads for control input (keyboard, serial)

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    101

    Using threads for control input (keyboard, serial)

    I have a version of my app that is using threads to wait for keyboard input and input from the serial port (one thread for each). Based on what input is gathered from these threads, they set a variable in the calling thread so it knows what to do. Both threads are set up to loop infinitely and wait for keystrokes (thread1) or data coming in from the serial port (thread2).

    The value they set in the calling thread's variable determines its course of action. The calling thread itself is in a loop (constantly reading this variable).

    This seems to be working okay so far, but is there a better way to do this? I suppose I could put the checks for key and serial port input in the main loop, but what advantage would that offer? As I mentioned, the main loop just constantly checks a variable, so what does it matter whether it's set by its own loop or by some other thread?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Threads seem like massive overkill. Use select().
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. Catching All Keyboard Input
    By nigma in forum C Programming
    Replies: 6
    Last Post: 06-11-2005, 02:35 AM
  3. Keyboard input in a Dialog
    By ksarkar in forum C++ Programming
    Replies: 1
    Last Post: 05-20-2005, 05:39 AM
  4. getting input from keyboard, passing to execvp
    By jumpyg in forum C++ Programming
    Replies: 4
    Last Post: 11-02-2003, 08:49 PM
  5. serial programming, threads and dynamic buffer
    By lectrolux in forum C Programming
    Replies: 1
    Last Post: 05-06-2003, 09:59 AM