Hi All, I'm working on a project just now that involves some heavy use of A* path finding, not something I'm unfamiliar with, but for this one I wanted to really push the boundaries and so I turned to multi-threading. Queue few months later and learning a lot along the way, I feel I have a good grip and understanding of multi-threading, it's caveats and issues, but at the same time, it works wonderfully, to the point I can easily have 1000 objects quickly path finding round complex paths on fairly large maps, with zero slowdown on the main thread. I only use a couple of threads, purely for doing the heavy data sorting mainly. I'm now at the point where I could push it even more, but what I'd like to ask is if I should... I'm curious on what your thoughts on multi-threading are, is it something that's "ok" to use, or are there any problems you may know of that I'll run into in the future, say for example hardware (targeting PC/Linux/Mac only), or is multi-threading just simply frowned on? Thanks, Geordie
Nothing explicitly wrong with multithreading. A few things worth considering: The OS must spend time scheduling threads. So just adding more threads doesn't always mean an absolute performance increase The Unity API is not thread safe, so you have to push work back to the main thread Deadlocks and race conditions must be considered Put that all aside and many large games would not be possible without multithreading.
My only problem with multi-threading is that, like reflection, so many people who barely have a grasp of the basics will try to jump right into it (and inevitably end up here). That, and it's annoying when people get angry at the fact that Unity isn't thread-safe and take it out on the ones trying to assist them.
I could understand why they would (come here, not get angry at those that help...), there's not a whole lot of information specific for Unity regarding threading, and it can initially be a right mind field when they do jump in, which is a shame because from my experience the past few months, it's an incredibly powerful tool for doing complex background stuff. Thanks for the input BoredMormon, I understand your points, I was just looking to see if it was a big no no or not before I push deeper into the development, sounds like it's ok.
Because there isn't much specific to Unity about threading, but some people seem to not accept that as an answer The only thing to remember is, as BorderMormon and Lysander said, Unity API should/can be accessed only from the main thread. That's essentially it. Everything else is the same as with any multi-threaded application. When you acknowledge that, any C# manual/tutorial/book that touches multi-threading is relevant to the discussion and there are tons of sources to do almost anything with threads. I'd say especially MVC/MVVM and WPF were useful when I was researching the topic (in short - how to separate UI thread from actual data processing).
This. If you are looking for Unity specific information on threading it comes down to these two points The Unity API is not thread safe, and can only be accessed from the main thread Threads should not be used in Unity to provide timed events, for this you should use a Coroutine, Invoke or InvokeRepeating. Everything else is pretty much standard C# stuff. Note the same thing goes for many other advanced coding techniques. Once you move away from the Unity API, everything handles the same way you would expect it too in a pure C# environment.
Thanks for the input all, I'm not really after tutorials, I do understand the whole moving away from the Unity API, timing etc, I was more just looking to see if it's something I "should" be using or if there are any known hardware limitations from threading on different platforms, like Linux or Mac, I'm only working with PC currently.
Just to add a point of specificity in regard to the API - you can use portions of it in other threads but you can't manipulate the game world. For example, because @DigiScot mentioned A* - you can do vector math in another thread using Unity's vector stuff but you can't, say, move a GameObject to a given position.