Search Unity

Thread safety, Allocator.Temp,

Discussion in 'Entity Component System' started by RakNet, Apr 3, 2019.

  1. RakNet

    RakNet

    Joined:
    Oct 9, 2013
    Posts:
    315
    How is thread safety enforced when using ParallelForTransform?

    "The C# Job System solves this by sending each job a copy of the data it needs to operate on, rather than a reference to the data in the main thread. This copy isolates the data, which eliminates the race condition."

    This is fine for arrays of floats, but there is ParallelForTransform. Once I have a transform I can write code such as transform.gameObject.GetComponent<X>() and from there I can call functions in a non-threadsafe way. Is it intended that I'm responsible for my own thread-safety in this case, such as by using critical section locks?

    "Allocator.Temp has the fastest allocation. It is for allocations with a lifespan of one frame or fewer"
    Within the same frame or until the next frame? What qualifies as a frame, is that Update() to Update()?

    In RaycastCommand.ScheduleBatch it says "The first invalid result is identified by the collider being null" However, what about if I wait several frames before checking the results, and an earlier collider in the array had been destroyed for other reasons? I would have other valid results but wouldn't know it.
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    IJobParallelTransform automatically sets a dependency on that transform hierarchy that any further reads / writes will depend on. So it is automatic.
     
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Don't missing first part:
    If maxHits is larger than the actual number of results for the command the result buffer will contain some invalid results which did not hit anything. The first invalid result is identified by the collider being null.

    All this paragraph only about maxHits which anyway not working yet, and only returns first hit.
     
  4. RakNet

    RakNet

    Joined:
    Oct 9, 2013
    Posts:
    315
    I'd like to clarify, from a transform I can get a component, and from there I can call any code in my entire game. Is it doing some kind of code analysis and automatically locking critical sections based on that?
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You can not get another component from IJobParallelForTransfrom. You get access only to the transfrom data itself using the passed in TransformAccess struct.

    We do not use critical sections or locks. Mutexes and atomic contention are not compatible with "performance by default". We use dependencies to guarantee exclusive access and enforce it through API design and DOD style code working in buffers. Think of a Transform Hierarchy as a NativeArray from the perspective of the C# job system.
     
    psuong and eizenhorn like this.