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

FEEDBACK:
Please add nativeThreadIndex to the Job.WithCode() delegate, like so:

        Job.WithCode((int nativeThreadIndex) =>
        {
            Debug.Log($"Job.WithCode() threadIndex={nativeThreadIndex}"); 
        })
            .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)

public class ExampleSystem : SystemBase
{

    [Unity.Collections.LowLevel.Unsafe.NativeSetThreadIndex]
    public int nativeThreadIndex;


    private struct ExampleJob2 : IJob
    {

        [Unity.Collections.LowLevel.Unsafe.NativeSetThreadIndex]
        public int nativeThreadIndex;

        public void Execute()
        {
            Debug.Log($"ExampleJob2 (IJob) threadIndex={nativeThreadIndex}");  //1 to 16.  never zero.
        }
    }

    protected override void OnUpdate()
    {
        var nativeThreadIndex = this.nativeThreadIndex;
        Job.WithCode(() =>
        {
            Debug.Log($"Job.WithCode() threadIndex={nativeThreadIndex}");  //always zero
        })
            .Schedule();

        var example2Job = new ExampleJob2();
        example2Job.Schedule().Complete();
    }
}

Does it work if you write it like this?

Job.WithCode((int nativeThreadIndex) => {

}).Schedule();

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: Using Job.WithCode | Entities | 0.16.0-preview.21

If you want to have multiple thread running your block you should use ScheduleParallel

[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):

Job.WithCode((int nativeThreadIndex) => {
}).Schedule();

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.

1 Like

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

1 Like