I'm trying my hand at multi-thread C programming and decided to start with a simple ncurses program to move a character across the terminal screen and change direction based on user input (think classic snake game). I decided that I needed to create a separate thread to handle moving the character across the screen, while the main thread handles capturing user input. My question is as follows:
Is there any benefit one way or the other to having the user input captured in the main thread, or the child thread? Does it matter at all which thread controls coordinate placement and which one controls capturing user input? Is there a best practice?
Pseudo code:
int main()
{
pthread_create(blah....)
while(ch=getch())
/*
get user input to change direction
*/
}
void *thread (void *arg)
{
while(true)
/*
update placement of character on screen
*/
}
// is there a best practice for which thread
// handles which function?
EDIT:
Thank you to everyone for your responses. In case it's useful for other's in a similar situation, ncurses' function nodelay(stdscreen, TRUE) will put getch() into "No Delay" mode. This will constantly poll the keyboard, allowing you to put all of your logic in a single, main thread. Thank you to everyone that helped guide me to this design.
My original question focused on an issue that I had during multi-threading. While I believe it is valid and a great learning opportunity, some of the other responses guided me down a completely different approach to my program. The correct answer for the original question comes from:
@Blrfl
The correct answer for the question that morphed out of my original ("What is the best way to write a game loop accepting user input"), came from:
@Dragon Energy