Hi, here is what i have: - A Monster in the scene - A script PathFinder attached to this monster My pathfinder script add a thread to a threadpool each time it needs to recalculate a path. I have no problem with that, every threads seems to be stopped when the path is calculated. But, when i put multiple monsters, only 3 or 4 of them are moving and the other ones are just doing nothing and my Unity Process uses 100% of my CPU. Script attached to the monster: Code (CSharp): pathFinder=newPathFinder(); ... if (Vector3.Distance (newTargetPosition, oldTargetPosition) >= 2f) { if (pathFinder.finished) { pathFinder.SearchPath (transform.position, newTargetPosition, wmask, ref waypoints); } oldTargetPosition = newTargetPosition; } .... PathFinder script: Code (CSharp): public void SearchPath(...) { finished = false; ThreadManager.AddThread (new WaitCallback (Ascend)); } private void Ascend() { discoveredList.Add (startNode); /* JOB */ finished = true; } And here is my ThreadPool: Code (CSharp): public class ThreadManager : MonoBehaviour { // Use this for initialization void Start () { SetupThreadPool (); } void SetupThreadPool() { ThreadPool.SetMaxThreads (Environment.ProcessorCount, Environment.ProcessorCount); } public static void AddThread(WaitCallback b) { ThreadPool.QueueUserWorkItem (b); } } Thanks for your support !
Whats not showing with this code is how often you are recalculating your paths. How often, per monster, do you recalculate its path? Do you do it only whenever something significant changes or are you path finding every frame?
if (Vector3.Distance (newTargetPosition, oldTargetPosition) >= 2f) { I recalculate the path each time the target position move by 2 from the previous position
Have you verified that with Debug.Log() that it's not being called a thousand times per second? Are you calling newPathFinder (a constructor?) every frame?
My problem was that the thread were overwriting themselves, i've put a locker in my Ascend() and everything is fine now.