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. Dismiss Notice

Multithreading vs multiple cores

Discussion in 'General Discussion' started by DarthHawk13, Apr 3, 2022.

  1. DarthHawk13

    DarthHawk13

    Joined:
    Feb 24, 2016
    Posts:
    63
    Let's say I compiled a game with Unity and didn't program it to use multiple threads... how many cores will the game made with Unity use on a player's computer?

    From what I've been studying Unity's API is thread safe, meaning it can't be used in multiple threads. However, other game data that doesn't use Unity's API can be used in multiple threads. And then there's Unity's job system.... If I understand correctly, and maybe I don't, which makes it so you can use Unity's API in multiple threads. So now I'm confused.

    I'm also wondering if using more than one core and multithreading are the same thing? Maybe I'm getting hung up on terms.
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    Your scripts will only use one core. The actual engine itself though is not limited to just one thread therefore it won't be limited to just one core. Just as an example I created an empty project and made a build. Task manager reports over 200 threads.

    UnityGameTaskManager.png

    Most of these are not doing anything important. In fact many of them exist for the sole purpose of just monitoring for a specific event or situation to occur like receiving input from your keyboard, mouse, etc. DirectX 11 and Vulkan can both take advantage of multiple threads to speed up rendering so those systems can use multiple cores.

    No. Under the hood Unity's Job System is just normal threads. A few things can be passed through to the job like transforms but the majority of the Unity API is still off limits. The only way around this limitation is to use ECS and its supported packages with the Job System as they are made to work with multiple threads.

    Multithreading enables the use of more than one core but you don't have to have multiple cores to use it and in fact multithreading has been around since the 1950s long before we had multiple cores in a single computer.
     
    Last edited: Apr 3, 2022
    Meltdown likes this.
  3. DarthHawk13

    DarthHawk13

    Joined:
    Feb 24, 2016
    Posts:
    63
    Thanks. After reading other articles I realize I've been thinking a thread is the same as a processor which is not the case.
     
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    Something to be aware of too is that modern processor cores often can handle more than one thread at a time. On desktop each core can typically handle two threads. On servers there are some that can do eight per core.
     
  5. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,459
    You got this part exactly the other way around. The Unity API is NOT threadsafe and that's why it cannot be used in multithreading.
    It is safe in regards of recognizing when it's attempted to be used from multiple threads and then throws an error immediately.
     
    angrypenguin likes this.