Jump to content
  • Advertisement
Sign in to follow this  
X3non

Question(s) about multithreading

This topic is 3590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing a simple multithreaded project, i have 2 threads, one for rendering, and one for logic/simulation. I want the logic/simulation thread to be updated as soon as possible, but i don't know how to handle resources between two threads. I'm thinking of copying the data from one thread to another (data from sim thread to render thread), it would look something like this: |--sim-+-copy--|--sim-|--sim-+-copy--|--sim-|--sim-|--sim-+-copy--|--sim-| .... |-wait-+-copy--+--render-----|-copy--+--render-----|-wait-+-copy--+--render ... there isn't a lot of data, so copying shouldn't take long, but is this the right approach, is there another way. btw, I'm using SFML library for threading and rendering and i would like to know which thread should be in the main thread (the other one would in it's own separate thread). SFML uses OpenGL for rendering, can i use OpenGl in another thread ? Please help, and thanks in advance for your time. p.s. sorry for my English

Share this post


Link to post
Share on other sites
Advertisement
Looking at your little ascii diagram there, you spent more time waiting and copying than you save by using multiple threads - and if the application is ever run on a single core system, it will be that much worse.

Share this post


Link to post
Share on other sites
Your approach is OK.
I use the same and got really nice results (up to 80% increase when CPU heavy).
Though your copying methods have to be carefully thought. You don't want to spend much time on copying.

There's something wrong with your assumption however, and has nothing to do with multicores.
You want your simulation to be updated as fast as possible, and make the render thread wait for it.
IT'S THE OPPOSITE!!!
Google for fixed-frame rate steps. You want to render as fast as you can, and simulate at fixed steps, constant intervals.

Cheers
Dark Sylinc

Share this post


Link to post
Share on other sites
Just some idea, but can't you use the old double buffering technique to prevent copiing? For example, you have two position vectors p[2]. p[0] is simulation input for even frames and p[1] is simulation output for the even frames. For odd frames, its the other way around. When rendering, you always render from the current simulation input position.

just an idea, might work

Share this post


Link to post
Share on other sites
I believe what dietpiet mentions is what Quake4 does. It's indeed a good idea.

One thread begins rendering with data off one buffer while the other does physics/logic on the other buffer. Then they switch. Although, I doubt it's quite that simple, it sounds it enough.

Alternatively, you could partition your buffers. The copy step could copy only half of the physics data to the render buffer, then the render thread could start working on that data while the rest is copied.

And, as has been said, you want to simulate at fixed time steps. A good article: "Fix Your Timestep" (google it). One problem with a variable one is when physics updates are frequent, accuracy is supposedly high; when the game is slow, accuracy is low. So you could lob a ball and find the arch is different depending on performance. (Although some integration techniques will do alright, as long as it's not Euler.)

Share this post


Link to post
Share on other sites
Thanks everyone!

The double buffering technique sound good, but but the implementation may be tricky. In my previous projects i have used variable time steps, and I've split up the time that has passed to get about desired number of iterations in one second. For example, i want the game physics to be updated 2000 times in one second, when a i call update(double time_passed) it calculates the how many time the function needs to update physics: the formula is N = 2000 * time_passed,
then i update the physics in a for loop the time step is close to 1/2000 seconds. I think that this is something close to Euler integration (please correct me if I'm mistaken). This approach has worked quite well (the physics was precise and stable), but now I want to use the powers of 2 or more cores in almost every modern computer.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!