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

Recommended Posts

Well, I've been looking into multithreading to do a single (but tedious) task. I am creating a space sim that allows the player to travel between stars. During the travel time, I will be generating the "new" star system. This task creates all the planets ,the terrain texture, and the atmosphere textures. Needless to say, this is a HUGE drain on the program; so I broke it up into passes. This works ok, but I still run into systems with a lot of planets, so this takes several thousand passes to get everything generated. This means that some times I reach the system before everything is generated and I had to make a "while" statement to finish the generation.....

I have never done multithreading before and I don't know how to make it work. The examples seem to want me to create the thread and have it exit when finished.

DWORD WINAPI MyThreadFunction( bool Exit ){
tp3++;

return 0;
}



(Ignore the "bool Exit" parameter).... This will only increment tp3 once, and that makes sense but when I do something like:

DWORD WINAPI MyThreadFunction( bool Exit ){
while (true) tp3++;

return 0;
}



It does nothing at all......

tp3 is a global variable I use in the program. Here is the creation:

I was thinking, perhaps, I should do the thread creation whenever I need to generate and just leave it at that.... Would that work? I don't want to create memory leaks or something like that.....

Share on other sites

With while() loop compiler probably put variable into register, so value in memory will never be updated until function is exited or significant part of other code is executed. You can fix this by making variable volatile:

volatile int tp3 = 0;


Share on other sites

Also, look into mutexes and release the lock as soon as you get the data you need from the variable.

I don't know enough to know what that means..... Please explain.

Also, I was wondering if this is ok:

//****** outside the game loop
//************* generate solar system here **********
return 0;
}

//*********** inside the game loop
if (GenSys){
GenSys=false;
}


The GenSys is only true on initial "traveling to a new star".

EDIT: I don't think the "shared" data is going to be a problem. The data is only used by other parts of the program (like rendering) once it is fully generated.

Edited by Hawkblood

Share on other sites

Ok. I tried to use std::thread, but I don't have c++11.... I'm trying to find a way to get the library.....

Share on other sites

I downloaded VS2012, but I can't seem to get my program to compile on it. I am sure I DON'T have it set up right......

EDIT: I must have been doing something stupid. I got it working now...... So, with what I described, does anyone have a SIMPLE example of how to actually do multithreading?

Edited by Hawkblood

Share on other sites
Threads are not so hard people make them to be. If I understood right about what you want to do this is the easiest multithreading case there is. Correct me if I'm wrong.

You want to have another thread generate your starsystem while your main thread is handling the game. Without the additional thread you would do a small portion of the generation between each frame and the performance is not good enough for you.

There is no data racing or any other complicated stuff going on if you generate the starsystem in a single thread and make sure you do not access the starsystems data from anywhere until the generation is complete. This kind of threading does not take that much effort to get it right and will increase your games performance quite a bit. You only need to have a threadsafe way to tell the main thread if a starsystem is still being generated. You also can use n generation threads to generate n starsystems at the same time.

My tip is to not use any global variables to manage the threading as those get complicated rather quickly. Have a "generated" flag that's defaulted to false and set it true after generation is complete in your starystems class or struct or whatever you use to store your starsystems in memory. You may add a memory barrier to after the starsystem is generated to make sure all threads have the same data.

This way you'll have a full thread generating the starsystem as fast as possible without any need for complicated threadmanagement stuff. A lot of people are trying to scare people away from threads because they don't understand it themselves. Granted there are a lot of complicated stuff to do if you want to multithread realtime simulations but a simple background thread to generate the next part of the gameworld before the player is close enough to interact with the part is a simple task.

You seem to have managed to get a thread going. Just try and put the code to generate a starsystem into the thread and see what happens. If something gets broken and you can't figure out why then come back here and explain what and how and someone will propably be able to help. Multithreading is not rocketsience.

• 12
• 18
• 29
• 11
• 24