Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Update() method: No concurrent execution (?)

Discussion in 'Scripting' started by plmx, Sep 8, 2016.

  1. plmx

    plmx

    Joined:
    Sep 10, 2015
    Posts:
    308
    Dear all,

    just a quick question: From reading the docs, it seems that if I have several MonoBehaviour-based classes on different (or the same) game objects, their Update() methods will be called in a random, but sequential order. Is this true? I.e. the Update() methods will NEVER be called concurrently (unless I play around with coroutines myself)?

    The reason I am asking is that I'm considering integrating several Update() methods and I was wondering whether this might potentially slow things down.

    It would be great if someone could confirm this.

    Thanks!

    plmx
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Yes. And coroutines aren't concurrent either; they're simply a mechanism to spread execution out over multiple frames.
     
    Ryiah and plmx like this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Ryiah, plmx and LiterallyJeff like this.
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    I believe Unity API is not thread safe, and thus can only do one thing at a time on one thread.

    You can use a Manager or Script Execution Order to determine the order of execution.
     
    Ryiah, plmx and Kiwasi like this.
  5. plmx

    plmx

    Joined:
    Sep 10, 2015
    Posts:
    308
    Thanks for all your replies, very helpful!! I can rely on non-concurrent execution in my Update() (and I guess also Start(), Awake(), etc.) methods then.

    This of course begs the question how to run things in a truly concurrent way (if coroutines aren't doing this, as was mentioned). I suspect I have to start threads the "old" (C#) way - right? And find a way to sync back to Unity's main thread afterwards, if the API isn't thread safe.

    By the way, I guess Unity is doing internal stuff in other threads to take advantage of multicore processors? E.g. physics or talking to the GPU or other stuff? Does anyone have info (link?) on the concurrency-related internal workings of the engine?

    Thanks!

    plmx
     
  6. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    I believe that the only thing that Unity is not putting into the main process thread is graphical (obviously, because it's put onto the GPU)
    From what I've read, that's all the multi-threading that Unity does out-of-the-box
     
    plmx likes this.
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Unity does a fair amount of multithreading where feasible such as skinning/Mecanim/cloth/particles/etc. A far as user code goes, you can use threads with thread-safe Mono/.NET code only and sync with the main thread.

    --Eric
     
    plmx, LiterallyJeff and Ryiah like this.
  8. plmx

    plmx

    Joined:
    Sep 10, 2015
    Posts:
    308
    Hi,

    thanks a lot! I'll take away the message that all relevant scripting methods for me are non-concurrent unless I resort to c# threads, in which case I should sync back to Unity's thread once I'm done. Unity does use multithreading but outside of the developer's influence.

    I can work with that. ;-)

    Thanks again!

    -plmx
     
    LiterallyJeff likes this.
  9. Brominion

    Brominion

    Joined:
    Sep 30, 2012
    Posts:
    48
  10. grimunk

    grimunk

    Joined:
    Oct 3, 2014
    Posts:
    270
    We use multithreading in Unity. Once you build a thread-safe method to communicate with the main thread it works rather well. Significant effort is required though, and I don't think Unity's profiler is as effective when you have other threads running.
     
  11. plmx

    plmx

    Joined:
    Sep 10, 2015
    Posts:
    308
    For the sequential execution order, yes; however, it doesn't address concurrency.

    Thanks for the experience report!

    -plmx
     
  12. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    A very light-weight way to do move stuff to other threads is through Thread Ninja. It's free too!
     
    plmx likes this.