Search Unity

Feedback How to get nativeThreadIndex from a Job.WithCode() ?

Discussion in 'Entity Component System' started by jasons-novaleaf, Jan 20, 2021.

  1. jasons-novaleaf

    jasons-novaleaf

    Joined:
    Sep 13, 2012
    Posts:
    181
    FEEDBACK:
    Please add nativeThreadIndex to the Job.WithCode() delegate, like so:

    Code (CSharp):
    1.         Job.WithCode((int nativeThreadIndex) =>
    2.         {
    3.             Debug.Log($"Job.WithCode() threadIndex={nativeThreadIndex}");
    4.         })
    5.             .Schedule();
    Right now, adding that is a compile error.

    ORIGINAL POST:

    When I run the following code, the nativeThreadIndex for the Job.WithCode() is always zero. I want to know what thread it is running on so I can have it properly access data in an shared array (one element per thread)

    How can I get the thread it's currently running on? if it's not possible, I hope that means it's running on the main thread? and if so, then what's the difference between .Schedule() and .Run()? (I mean, if they both run on the main thread, who cares if it's done now or some other time in the next 33ms)

    Code (CSharp):
    1.  
    2. public class ExampleSystem : SystemBase
    3. {
    4.  
    5.     [Unity.Collections.LowLevel.Unsafe.NativeSetThreadIndex]
    6.     public int nativeThreadIndex;
    7.  
    8.  
    9.     private struct ExampleJob2 : IJob
    10.     {
    11.  
    12.         [Unity.Collections.LowLevel.Unsafe.NativeSetThreadIndex]
    13.         public int nativeThreadIndex;
    14.  
    15.         public void Execute()
    16.         {
    17.             Debug.Log($"ExampleJob2 (IJob) threadIndex={nativeThreadIndex}");  //1 to 16.  never zero.
    18.         }
    19.     }
    20.  
    21.     protected override void OnUpdate()
    22.     {
    23.         var nativeThreadIndex = this.nativeThreadIndex;
    24.         Job.WithCode(() =>
    25.         {
    26.             Debug.Log($"Job.WithCode() threadIndex={nativeThreadIndex}");  //always zero
    27.         })
    28.             .Schedule();
    29.  
    30.         var example2Job = new ExampleJob2();
    31.         example2Job.Schedule().Complete();
    32.     }
    33. }
     
    Last edited: Jan 20, 2021
  2. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    Does it work if you write it like this?

    Code (CSharp):
    1. Job.WithCode((int nativeThreadIndex) => {
    2.  
    3. }).Schedule();
    4.  

    But the difference between Schedule() and Run() is:

    Basically Run() means, right now, on the main thread. Schedule() means, on some other thread, when the previous jobs are done, or a .Complete() is encountered.

    From the "Executing the function" section here: https://docs.unity3d.com/Packages/com.unity.entities@0.16/manual/ecs_job_withcode.html
     
    Last edited: Jan 20, 2021
  3. scarface117

    scarface117

    Joined:
    Nov 9, 2019
    Posts:
    16
    If you want to have multiple thread running your block you should use ScheduleParallel
     
  4. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    [Unity.Collections.LowLevel.Unsafe.NativeSetThreadIndex] is not supposed to be used at system level, this is why you are getting 0 with it. I am not sure if you can get the nativeThreadIndex using Job.WithCode, but you can try what RecursiveEclipse suggested and see if that works (or even compiles):

    Code (CSharp):
    1. Job.WithCode((int nativeThreadIndex) => {
    2. }).Schedule();
     
  5. jasons-novaleaf

    jasons-novaleaf

    Joined:
    Sep 13, 2012
    Posts:
    181
    Thanks, I tried that, it doesn't compile.

    Not trying to run it parallel, but I want to know what thread I'm on so I could enqueue work to another job in a threadsafe way.

    Maybe it's not possible, though I'd consider that a bug. I'll just use IJob instead.
     
    charleshendry likes this.
  6. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    It is not a bug, you shouldn't use those attributes meant to be used inside Jobs directly inside the SystemBase. Getting the nativeThreadIndex inside Job.WithCode seems to just not be supported at the moment
     
    jasons-novaleaf likes this.