Gameloops for Multicore Chipsets

Here is an improved version of the sim, that runs itself 100 times and averages out the frame times and sim times for comparison:
https://github.com/MrGrak/Gameloops/blob/master/CsharpConsoleAppExamples/mainWithJobsOptimized.cs

Here is a video where I compare a single threaded sequential simulation vs a 3 threaded concurrent simulation, using the code linked to above:

The difference between these two tests is 15 lines of code, which I think shows how flexible this job strategy can be. It can run single threaded, if the chipset has only one core, or it can run multi-threaded if the chipset has many cores. 15 lines of code describes -both- the single and multi threaded strategies for doing the simulations work.

In the video, I can see core cpu useage go up when I switch from doing simulation work on 1 thread, to using 3 threads - creating threads using ThreadPool.QueueUserWorkItem(a => Method());

“You’re not doing anything new, everybody knows multiple threads run faster.”
I know this is true, but I have yet to find any examples showing how to do this with a game loop, as I am trying to do here. I have looked. I have started asking people for comparable examples - using global classes with read write lists to handle single or multi-threaded architectures easily - and I have not found any. I think these are interesting ideas - based on mutability and global variables / objects / classes.

“But Global variables are bad.”
If used improperly, yes, I agree. But I contend they can be used efficiently, usefully, as ways to organize data that many different parts of your program may need to talk to. It is simpler. It reduces complexity, in my experience. It is easy to mis-use, yes. It’s like a lightsaber - I can accidentally cut off my hand, but I still want it. Public Static classes actually solve a lot of problems when you need communication between a lot of different code or different threads. They can group data, or they can group functions. I’m not talking about functional programming 100% here - I’m talking about putting functions that logically fit together (like, the drawing functions) into a public static class so they can be called when needed (like, Functions.Draw()).

There are many different ways to organize and structure your programs, and the branches of evaluation that can happen through them, and I feel like we (as programmers) have chosen to ignore what potential advantages global mutability has in our program designs. In all these examples, I have intentionally only used global program state and mutable design to show that it can be done. It can be done well, and simply, and it’s quite powerful for synchronizing threads, handling data, and doing work. The next game I’m going to make will use this as it’s core, due to the levels of flexibility it provides to me.

“You can’t make a game like that, with global mutable design. It will be a mess.”
I already have: www.github.com/mrgrak/tsk
I’ve been at it for almost 2 years now, actually.

I’d like to have that talk now, please.

1 Like