The way I set things up (usually) is that my game runs on its own thread unrelated to unity and the unity side only handles game data rendering and user input.
I’m currently implementing a pathfinding system(A* on hex grid) and I’m not sure what to do with regards to threading it, the game is already in a thread and I have no idea how starting a thread from a thread works.
is it completely fine and shouldn’t be even considered a problem? when I try googling I only find information on how to start threads (101 threading tutorials)
what if my game thread is created on application start and it’s the same thread all the game time (a threaded function with a while loop)
what if I launch a new thread every game update (so the same function with out the while loop)? does it matter if it’s a new thread or from the thread pool?
as far as I have to guess it’s a fine thing to do, but please someone enlighten me on any gotcha’s here.
Threads are threads. That includes the main thread that the game loop runs on. Starting a thread from a thread is the only way to start a thread.
Probably a bad idea. Creating and managing threads is not free. There’s a significant overhead involved in creating a new one.
Thread pools mitigate the cost of creation and destruction of threads. It’s like recycling them. It’s generally a good idea to use a thread pool to avoid the overhead of creating new threads if you are doing lots of short-lived tasks.
The main gotcha about multithreading in Unity is that you can’t read or write the vast majority of Unity objects from another thread. You need to be extra careful about data boundaries between threads for this reason.
1 Like
Yeah I know about multithreading in general and the do’s and don’t, I use a thread pool and when I say a game update I don’t mean every unity rendering frame (the mb.Update loop), i mean on my own clock that runs a frame every few seconds (but I still pool that thread), the question was just a hypothetical(oh wow did I get this word right? no red line so either that or it’s a different word lol) situation to understand what’s happening
What I was asking about is if the callstack(?? or whatever) does something funky and undesirable the more levels you go into threading inside a thread, if that makes any sense, haha
the gotcha’s i was talking about was with multithreading in general in the way i described, not multithreading in unity, that’s a chewed out topic.
Nope. Each thread has its own independent callstack. That’s kind of the point!
1 Like
Maybe I’m not using the right word, but thanks for alleviating most of my concerns.
Just to be clear, when starting a new thread it’s doesn’t care at all who started it or why, only what process it’s attached to?