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();
}
}