Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Error when reading from Readonly NativeArray from different jobs running same time

Discussion in 'Entity Component System' started by Maverick, Jul 15, 2019.

  1. Maverick

    Maverick

    Joined:
    Dec 18, 2009
    Posts:
    240
    Hello,

    So I have following scenario:

    I have a native array defined and initialized in main code. Something like:

    Code (CSharp):
    1.  
    2. public NativeArray<int> myArray;
    3.  
    Then I have 2 jobs defined, something like:

    Code (CSharp):
    1.  
    2. public struct MyJob : IJob
    3. {
    4.             [ReadOnly] public NativeArray<int> data;
    5.             public NativeArray<int> result;
    6.  
    7. ...
    8.  
    9. }
    10.  
    I'm calling them as follows:

    Code (CSharp):
    1.  
    2. var job1 = new MyJob()
    3. {
    4. data = myArray,
    5. result = myResult1
    6. };
    7.  
    8. job1.Schedule();
    9.  
    10. var job2 = new MyJob()
    11. {
    12. data = myArray,
    13. result = myResult2
    14. };
    15.  
    16. job2.Schedule();
    17.  
    Please note above code is just for reference.

    I'm getting an error "The previously scheduled job ... writes to the NativeArray data. You are trying to schedule a new job ..., which writes to the same NativeArray".

    The question is how can I use same native array just for reading data in different jobs running in parallel? Is it allowed? Or, do I need to copy to a local array and pass it separately for each job? The manual stipulates that "The safety system does allow multiple jobs to read from the same data in parallel.", but I cannot make it run for some reason.

    I'm using Unity 2018.4.2f1

    Thanks
     
  2. Maverick

    Maverick

    Joined:
    Dec 18, 2009
    Posts:
    240
    Use [Unity.Collections.ReadOnly] instead of just [ReadOnly], that might save you some time.