2 tasks simultaneously in a C program

adroit89's Avatar
Go4Expert Member
can any one tell me how we can perform 2 tasks simultaneously in a C program.i.e.,to read a character during runtime.
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Use a more descriptive subject line and read the "Before you make a query" thread. Then think. Then ask what you really want to know. Most characters are read during runtime, doanchano.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by adroit89
can any one tell me how we can perform 2 tasks simultaneously in a C program.i.e.,to read a character during runtime.
What are the two tasks?
adroit89's Avatar
Go4Expert Member
i am making a letter fall from top to bottom of screen.during the time of fall i have to accept a character from user and check it with the letter falling.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Edited the thread for a better heading.
shanku_4ch's Avatar, Join Date: Jan 2007
Light Poster
actualy me & adroit r programming for individually for
TYPE WRITER GAME
in this game we r making each letters fall fall from top to bottom in a loop
but for the game we need to accept a charccter in same loop only once during the time of fall but not for each & every iteraton of loop .i hope u got us??
ever_thus's Avatar, Join Date: Jan 2007
Contributor
Is the letter to continue dropping while the program waits for the input? If so what you want is a multithreaded program.

I'm not going to post the complete solution here because there are many ways to do this. However I'll give you the concept. In a multithreaded program one thread does one task while another thread does another. So in this case, before you start the loop you'll start a thread ("spawn a thread" in the jargon) that asks the user for his input. When he gives it the thread terminates.

Like I said there are many ways to acheive multithreading. The simplest is to use Windows's CreateThread function (documentation available online). If you want to your program to work on other OSs you should download a C thread library. (I don't know of any I can recommend).
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Actually, you don't even need a thread. You can have character input raise an event. Neither threads nor events are a part of the C/C++ languages, so how you do this is implementation dependent.
ever_thus's Avatar, Join Date: Jan 2007
Contributor
True, that is a much neater way of doing it. Look here if you have VS.NET and here if you don't.