Mark MacDonald
Mark MacDonald

Reputation: 11

boost two threads

C++ Boost question about loops.

So I've been looking over as much information as I can and still not seeing any examples of what I am trying to do or the principles of how it works.

I've been working in my spare time for a few years on designing a game in C++. I got my Core engine for the game logic all figured out as well as a rough Input system and using OpenGL and AL for output. What I want to do is figure out how to get my Engine to start and Then run my Input System, Graphics Engine, and Sound System in separate threads. and all run at the same time. Syncing my threads be the next step but I can't get the threads to run together.

boost::thread gTrd(boost::bind(&game::runGraphics, this));
gTrd.join();
boost::thread sTrd(boost::bind(&game::runSound, this));
sTrd.join();
boost::thread iTrd(boost::bind(&game::runInput, this));
iTrd.join();
boost::thread cTrd(boost::bind(&game::runCore, this));
cTrd.join();

That's What I got so far. Problem is as far as I can see is that the Graphics Engine in gTrd has an infinite loop thats suppose to keep going until program terminates so I get my blank screen up but it never starts the sTrd.

What exactly is needed to make it so I can run my threads which are in theory indefinite threads? Also any potential problems I need to look out for in terms of memory leaks be awesome to know.

Upvotes: 0

Views: 317

Answers (3)

Martin James
Martin James

Reputation: 24847

Why not just dump all the join() anyway?

boost::thread gTrd(boost::bind(&game::runGraphics, this));
boost::thread sTrd(boost::bind(&game::runSound, this));
boost::thread iTrd(boost::bind(&game::runInput, this));
game::runCore();

Upvotes: 0

eladidan
eladidan

Reputation: 2644

You should only join() all the threads once you expect them to stop running. As you said, each thread is running in some sort of endless loop, so when you call join() before asking the thread to stop its run, your main thread will never resume its run.

Instead you should first tell the threads you expect them to finish their run. Here's, in pseudo-code, a simple mechanism that does what you want:

RunGame() 
{
    initialize_all_threads(); //just like your sample code does minus the join functions
    ...//do stuff while game is running
    wait_for_quit_signal_from_input_thread(); //note that the input thread is the only thread which transfers messages back to the main thread
    //quit signal received, so we should quit game
    signal_all_threads_exit(); //via some kind of flag/synchronization object which all thread "main loops" are listening on
    join_all_threads(); //wait for threads to gracefully end, perhaps with a timeout

}

Upvotes: 0

Jesse Good
Jesse Good

Reputation: 52355

Do you know what join() does? When you call it, it blocks the main thread until the thread that called join finishes. In your code you start up one thread, call join to wait until it finishes, and then start up another thread and repeat this process. Either call detach() to allow execution to continue (and you don't care when the thread finishes execution) or call join() after starting up all the threads (depending on your needs).

Note: You only want to call join when you want to wait until the thread finishes execution.

Upvotes: 1

Related Questions