Search code examples
javamultithreadinglwjglgame-development

Multithreading questions for game development?


I am writing a game with LWJGL3 and have a few questions regarding the concepts of multithreading.

Q1: I know that you shouldn't add multithreading if you don't need it because of the added complexity but when and how would you know you need it?

Q2: Is it something you would add in from the beginning of development or add it later when you need it?

Q3: What specifically are the coding challenges that are faced when multithreading. I am thinking about separating the main and rendering threads on my game but i'm uncertain as to why exactly multithreading would make this significantly more difficult. I know you don't have a guarantee to how long a thread will take to run but this seams like it would only be a problem if you had 2 threads that were doing logic not one that does logic and the other that does rendering?

Q4: Is using a message queue to communicate between threads suitable for games or is this overkill?

Also it would be great if you could share any good resources that you think would be helpful.


Solution

  • Q1: Usually you use multithreading whenever some sort of processing is going to take a while that you don’t want to hang your UI. If all your code is on the main thread then your UI becomes unresponsive in the time between the time consuming process starting and ending. If you need to do any Network Calls, you definitely need to put them on a different thread, and Android and Swift won’t even allow you to perform a Network call on the main thread.

    Q2: It doesn’t matter too much as long as you do it. If you know you’re gonna use it you can implement it as you go but there’s not much stopping you from multithreading after the fact.

    Q3: Many of the coding challenges have to do with timing your data and avoiding any Null Pointers since if the data you’re trying to access hasn’t arrived yet, your app can crash. Completion handlers are useful to ensure that certain functionality only happens after the data has arrived.

    Q4: If you mean like consistently passing all the data between the threads it’s kinda unnecessary, you only need to pass the data across that you need at a certain point in time.