Multiple cores became a standard. Even my old computer has four of them. What’s really disappointing though, apps still do not utilize it that match. Of course, I’m not talking about discord or office products. Using concurrency here would actually slow down things, but not even that is important as they do not need performance all that match. However, big performance demand is on different kinds of apps like games.

Games are indeed unique programs. They have to get everything done as fast es possible to offer best experience. At first, standard was 30 FPS then it moved to 60 and now it is slowly moving towards 120. Nevertheless, games clearly focused on huge number of entities are still running on one thread. Good example of this is game called Mindustry. Shortly a sandbox tower defence game written in java. It only really uses one core if we do not take pathfinder into account. If you are really good at a game and survive enough waves, your computer starts crying from fewer as number of enemies and buildings increases. But wait a second, CPU usage is only on 25%, isn’t that a waste?

For instance, there is an easy and effective way to make your game use 100% if it needs to. It took some time to figure out but “Gate System” is a way to go. First then anything else though, we have to make it clear that not everything should be done concurrently. One natural hint is how match synchronization you have to use, as synchronization is always slow. Needless to say, pushing tasks to main thread may seem like a good idea, I even seen this approach countless times, but it’s also absolutely unnecessary. Running multiple different tasks at the same time is a bed design from its core!

Gate System

It introduces two states. Concurrent and non-concurrent. Things that have to be executed on one thread will get done during non-concurrent and everything else in concurrent. There are 2 rules. Always execute only one type of tasks, and obviously run only one thread or all of them. And to ensure that second rule applies, you have gates. The so-called gate can be imagined as door to space ship. It consists of two doors, inner and outer, they cannot be opened at the sabe time because everything would fly out of spaceship including crewmates. Now imagine entities that want to enter are threads. Outer door will open and let everyone to in-between area. And as all threads are waiting, main thread can execute what’s needed, then close outer door and open inner door. Because we are living in a world of loops our threads will appear at a front door once again after they executed all the tasks. Then if we want threads to process one tasks bud do not start another until all other threads are finished, we can place more gates that will synchronize them.

Now that we know what gate system is, let’s talk about when to use it and witch language is best for implementing it. Gate system thrives when you have to execute same update on multiple objects stored in collection. If you have 4 cores for example, core can execute update on every forth object. To be more specific, execute collision detection between first two gates. The next gate use calculated results to modify all objects. What if you know where collisions will happen and you just need to deal damage? Let every thread collect data to its own array and then stop them all on a gate, then just let main thread to execute damage dealing or rendering. Lats thing we should do is talk about a language.

Golang

Best way to show good go is in this matter is just showing the code. All you need for gate is two sync.Mutex and two sync.WaitGroups structs. First, we define a door:

Then we put two doors together and define a gate.

And there you have it, your game is now concurrent and will use all resources it can, which also makes a better framerate.