Search Unity

Issues with Multithreading

Discussion in 'Scripting' started by Chimera3D, May 22, 2015.

  1. Chimera3D

    Chimera3D

    Joined:
    Jan 27, 2012
    Posts:
    73
    I am attempting to write a multithreaded system for processing meshes and well it mostly works except I'm not seeing any performance gains over my old non-threaded method. To my understanding what my system should be doing is performing operations simultaneously, for example, if I run Function A and it usually takes 5 ms to run, if I run that same function four times but on four separate threads the main thread only has to wait (around) 5 ms for it to finish as opposed to 20 ms if I ran them all on one thread, successively. The way (essentially) I'm attempting to do this is shown in my code snippet below, mostly based off of the code found in duck's answer here:

    Code (CSharp):
    1. WorkItem[] workItems = newWorkItem[5];
    2. ManualResetEvent[] doneEvents = newManualResetEvent[workItems.Length];
    3.  
    4. for (int n = 0; n < workItems.Length; n++) {
    5. doneEvents[n] = workItems[n].doneEvent;
    6. ThreadPool.QueueUserWorkItem (workItems[n].ModifyGeometry);
    7. }
    8.  
    9. WaitHandle.WaitAll (doneEvents);
    10.  
    the workItems class looks like this (left everything that is irrelevant to my problem out [which is most of it]):

    Code (CSharp):
    1.     public class WorkItem
    2.     {
    3.         public ScriptName owner;
    4.         public ManualResetEvent doneEvent = new ManualResetEvent (false);
    5.  
    6.         public void ModifyGeometry (System.Object o) {
    7.    
    8.             owner.ProcessVertices (this);
    9.            
    10.         }
    11.  
    12.     }
    and at the end of my ProcessVertices function I do doneEvent.Set(). To my understanding this should work but WaitAll waits ~20 ms (function usually takes ~4 ms) and is run 5 times while I thought that multithreading it would mean that WaitAll would wait ~4 or 5 ms since everything finishes at around the same time. The code works fine otherwise, there's just no performance gain and it was sort of a hassle to rewrite my system for this multithreaded approach to work. Obviously I'm not very experienced with multithreading but I feel like this should work (but I do know what coroutines are and they're not really applicable here so please don't suggest that I use them instead [saying this just because I see this on many multithreading-related questions]). Also I know not to use the Unity API on another thread, the process vertices function is really just vector operations and things like that. How is the code in duck's answer able to run the functions simultaneously but apparently mine can't, what am I doing wrong?
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Threads aren't really going to help you much on a 20 ms task. There is overhead associated with setting up the threads, and managing them.

    Typically the point of threading in Unity is not to get extra performance, but to prevent complex calculations from blocking the main thread. Typically you don't wait in the main thread, you continue on with the running new frames until the calculation is done.
     
    Fajlworks likes this.