Click to See Complete Forum and Search --> : Running two things at once


TheBouleOfFools
03-29-2002, 09:43 PM
I was wondering if I could do thisin C++. It seems like a waste to just have the program go through and run everything 1 thing at a time. I want to be able to run 2 functions or so at once. Something like...

Say there's functions foo() and bar(). Function foo() has a while (true) loop in it that I want to just run continuously (which is why it's a while (true) until I manually quit the program. I also want to be able to run function bar(). Seeing as how the computer will go through the program one step at a time, it'll go to foo and never leave. I just want to be able to run foo() then have the computer go on with it's life while still running foo() but continuing to run the rest of the program.

Strogian
03-29-2002, 10:17 PM
Umm... I'm slightly confused. How is foo() related to bar()? If they're not related at all (which is what I am getting from your post), why not just have them be 2 different programs?

TheBouleOfFools
03-29-2002, 10:21 PM
Well... Say I have a dumb program like this:

void foo()
{
while (true)
{
/*do some stuff*/
}
}

void bar()
{
/*do some stuff*/
}

void main()
{
foo();
bar();
}

If that program were to run it would just go to foo() and get stuck there and never leave. I just want it to start up a foo() process and then continue on to run bar(). The problem I have is in school in my free time during AP Programming I'm making Pacman and I was trying to get it's mouth to open and close but it would just get stuck while going through the the mouth thing and pacman couldn't move around. It would just sit there and open and close the mouth forever... which is fine but I don't want it JUST doing that. I want it to be able to open and close the mouth in the background while doing other stuff.

kmj
03-29-2002, 11:18 PM
umm assuming foo and bar consisted only of while loops, why don't you just do the following:



foo() {
/*do some stuff */
}

bar() {
/* do some stuff */
}


main() {
while (1) {
foo();
bar();
}
}




Now, as for maintaining state within foo and bar, you have a few options... if you're using c++, you should use member variables or local static variables (local to the function; this makes them "remember" their values from function call to function call). If you're using c, you should use "module" static variables (global variables declared static, so they will remain local to the file they're used in) or local static variables.

This basically does what you're trying to do, and makes the two loops take turns.

Otherwise, you'll have to use multithreaded programming which is probably overkill

Bradmont5
03-29-2002, 11:25 PM
If you want multithreading, chehk this out: http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html

bwkaz
03-29-2002, 11:29 PM
But multithreaded programming is FUN!

... or... uh... something. ;)

TheBouleOfFools
03-30-2002, 12:01 AM
thanks Bradmont that's what I was trying to get at :o