Search Unity

MultiThreading Freeze

Discussion in 'Scripting' started by Skylermaki, Nov 15, 2016.

  1. Skylermaki

    Skylermaki

    Joined:
    Jul 21, 2013
    Posts:
    17
    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):
    1. pathFinder=newPathFinder();
    2. ...
    3. if (Vector3.Distance (newTargetPosition, oldTargetPosition) >= 2f) {
    4.     if (pathFinder.finished) {
    5.         pathFinder.SearchPath (transform.position, newTargetPosition, wmask, ref waypoints);
    6.      }
    7.      oldTargetPosition = newTargetPosition;
    8. }
    9. ....
    PathFinder script:
    Code (CSharp):
    1. public void SearchPath(...) {
    2.     finished = false;
    3.     ThreadManager.AddThread (new WaitCallback (Ascend));
    4. }
    5.  
    6. private void Ascend() {
    7.  
    8.     discoveredList.Add (startNode);
    9.     /* JOB */
    10.    finished = true;
    11. }

    And here is my ThreadPool:

    Code (CSharp):
    1. public class ThreadManager : MonoBehaviour {
    2.  
    3.     // Use this for initialization
    4.     void Start () {
    5.         SetupThreadPool ();
    6.     }
    7.  
    8.     void SetupThreadPool() {
    9.         ThreadPool.SetMaxThreads (Environment.ProcessorCount, Environment.ProcessorCount);
    10.     }
    11.  
    12.     public static void AddThread(WaitCallback b) {
    13.         ThreadPool.QueueUserWorkItem (b);
    14.     }
    15. }
    Thanks for your support !
     
  2. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    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?
     
  3. Skylermaki

    Skylermaki

    Joined:
    Jul 21, 2013
    Posts:
    17
    1. if (Vector3.Distance (newTargetPosition, oldTargetPosition) >= 2f) {
    I recalculate the path each time the target position move by 2 from the previous position
     
    Last edited: Nov 16, 2016
  4. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    What happens if you increase MaxThreads?
     
  5. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    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?
     
  6. Skylermaki

    Skylermaki

    Joined:
    Jul 21, 2013
    Posts:
    17
    My problem was that the thread were overwriting themselves, i've put a locker in my Ascend() and everything is fine now.
     
    absolute_disgrace likes this.
  7. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Concurrency 101 :p