tags:

views:

199

answers:

2

Hi all, I'm working on a graphical application which looks something like this:

while (Simulator.simulating)
{
    Simulator.update();
    InputManager.processInput();
    VideoManager.draw();
}

I do this several times a second, and in the vast majority of cases my computation will be taking up 90 - 99% of my processing time. What I would like to do is take out the processInput and draw functions and have each one run independently.

That way, I can have the input thread always checking for input (at a reasonable rate), and the draw thread attempting to redraw at a given frame rate.

The simulator is already (internally) multithreaded and there is no issues with multiple threads writing to the same data (each one processes a segment).

My issue is I'm not sure how I can properly do this. How would I properly initialize my pthread_t and associated pthread_attr_t so that the thread runs without blocking what I'm doing? In other words, how can I create two threads, each of which run an infinite loop?

To generalize even more, I'm trying to figure out how to do this:

for (int i = 0; i < threads; i++)
    pthread_create(&th[i], NULL, func[i], NULL)

for (int i = 0; i < threads; i++)
    pthread_join(th[i], NULL);

Where func[i] is some arbitrary function which runs in an infinite loop doing some arbitrary thing.

Any help or even a link is appreciated, thanks!

Edit: I should mention it is an interactive simulator, so I do need to have two infinite loops running independent of each other. I can only seem to run at once.

+1  A: 

Double buffering is your friend here. Have 2 buffers of data. One is the drawing buffer and one is the calculating buffer. When you have finished calculating then wait for the current draw to finish and then swap the buffers over. Now it will continue drawing the newly calculated data while you are calculating the next frames worth of data. Drawing and Simulation are now almost completely de-coupled ...

Goz
Thanks, I already handle double buffering internally. My main issue currently is (essentially) trying to create two separate threads in main() that are both running an infinite loop.
Sagekilla
A: 

First I would suggest using boost::thread as opposed to pthreads since you are using C++. With boost::thread you can do something like this:

#include <boost/thread.hpp>

void input_thread()
{
    //...
}

void draw_thread()
{
    //...
}

int main()
{
    boost::thread input_th(&input_thread);
    boost::thread draw_th(&draw_thread);

    input_th.join();
    draw_th.join();

    return 0;
}

The constructor of a boost::thread automatically spawns a new thread and calls the function passed in. You can also use member functions as threads by using boost::bind. The join function blocks until the thread exits. This is necessary because if main() exits, all of your threads are killed.

Hopefully that will get you started, but the hard part is synchronizing (sharing data among threads). I suggest you look at the documentation for mutexes and condition variables. Remember that you need to make sure that only one thread is writing to the same memory location at once. Mutexes help solve this problem. Condition variables help by allowing you to signal and wait for signals between threads.

For instance, in the input thread you may fill a buffer with input then use a condition variable to signal to the draw thread that input is ready. In each thread, a mutex should be locked when accessing the buffer so that it is not overwritten by the input thread while the draw thread is trying to read it. As Goz suggested, a double buffer would make this easier and probably more efficient.

Jason
Thanks for the suggestion. I'll look into this. Thankfully, my problem threads nicely that I only have to split up work between separate threads and never worry about anything being overwritten.My only reason I was using pthreads was it was what I used previously, and a large portion of my threading code (as well as some legacy code I built off of) already used pthreads. Thank you though!
Sagekilla
Yeah, use what you are comfortable with. boost::thread actually just thinly wraps the pthread library on any *nix based system, replacing C idioms with C++ idioms (classes, RAII, etc...)
Jason