I’m building an Entity Component System framework, and I want it to be able to implement multiple cores on a computer. It’s not that any of the games I have in mind actually NEED the power of multiple cores. I just want to be able to give my framework that option since multi-core processing interests me.
My current update loop looks something like…
http://s8.postimg.org/ui6592dtx/single.png
Here, every function updates a part of the World, whether it’s an Entity, or a Property of the World. Every Function is called a System, and has a priority integer. The World sorts the order in which each System is stored based on its priority to ensure that certain Systems are called at a particular time in the update loop. Since some Systems have the same priority, however, they are open to being processed concurrently since the order in which they are invoked is arbitrary.
I WANT my game loop to look something like…
http://s8.postimg.org/mqpfai9ol/multi.png
Here, Systems with the same priority will be executed concurrently. My problem is that I don’t know how to conserve Threads. I’ve heard that it’s inefficient to create threads on the fly only to have them be immediately discarded, so what I want is to be able to reuse threads. I’m not exactly thread savvy, you see. I would like to be able to disable threads that are not in use, but not have them be discarded.
I want to write something like…
// If there's more than one System stored...
if(size > 1)
{
// Particular GameThread
GameThread threadly;
// Range of Systems with the same priority
int lowerBound=0, upperBound;
// For all Systems stored in a World...
for(int i=1; i<size; i++)
{
// If found priority upper bound...
if(systems[i].priority() != systems[i-1].priority())
{
// Track upper bound
upperBound = i;
// If upperBound - lowerBound == 1, then we've found a System with a distinct priority. Just run the sucker! :)
if(upperBound - lowerBound == 1)
{
systems[i-1].execute();
}
// Otherwise, we're dealing with multiple Systems with the same Priority. Time to program concurrently!
else
{
// Have GameThreads process Systems of same priorities concurrently
for(int j=lowerBound; j<upperBound; j++)
{
// Assign a System to a waiting GameThread, and have the GameThread execute
threadly = GameThreads.getThread();
threadly.setSystem(systems[j]);
threadly.execute();
}
// Wait for threads to finish updating somehow, then continue with the World's update loop
}
// Renew lower bound
lowerBound = upperBound;
}
}
}
// Otherwise, just execute the lonely sucker on our current thread :)
else if(size != 0)
systems[0].execute();
What this should do is group Systems of the same priority together, and have them execute concurrently.
Course, this is just theoretical code. What the hell is a GameThread, huh? It’s some sort of Thread, that’s for sure, and it is expecting a single System object as an argument. This is similar to a Thread expecting a Runnable. I’m not too sure how I would implement something like this with methods like wait(), notify(), or notifyAll(). My only knowledge on threads is how to use them for, say, loading resources, and game loops.