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

Question Make Jobs trust me

Discussion in 'C# Job System' started by Kragh, Feb 8, 2023.

  1. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    656
    EDIT: I thought I had look everywhere, but here it is (I think)
    Thank you
    ________________________________________________
    Completely new in jobs. Have done a lot of multithreading generally.
    I have a job that will write some data to an array. It will only write to parts of it. This array is persistent, and Native. Because of the way I set up multiple jobs, I am sure two jobs never write/read from the same part of it. So why is it that I get an exception? Can I overrule it somehow? Can I set a flag: "Trust me, I know I won't create any race conditions!!!"
    I want to segment the array into parts that can update in parallel, but I don't know how to, because Unity can't have the same NativeArray<byte> accross multiple jobs, without complaining. At least I have no idea how. I don't want to make dependencies, because then each job will wait for others to finish, which is completely unnecessary, because I KNOW I don't write to the same part. Help? Solutions?
     
    Last edited: Feb 9, 2023
  2. Mortuus17

    Mortuus17

    Joined:
    Jan 6, 2020
    Posts:
    105
    Together with NativeDisableUnsafePtrRestriction use GetUnsafePtr(). I think there might be another Attribute that does the same thing. Anyways, your jobs should now take pointers and the call to
    GetUnsafePtr()
    should be done once on schedule, since that method also performs thread safety checks.