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

DynamicBuffers,are they safe to write access in parallel ?

Discussion in 'Entity Component System' started by Opeth001, Oct 19, 2019.

  1. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,112
    Hello everyone,
    is it safe to write access a DynamicBuffer of the same entity in parallel jobs ?
    for me it seems obvious that it's not safe, but i read this part in the Docs
    "Accessing Buffers
    There are several ways to access DynamicBuffers, which parallel access methods to regular component data."

    if not, what is the best approach for writing to the same DynamicBuffer in multiple jobs ?

    Thank you!
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    I do buffer write and read in parallel all the time.
    You just need to guarantee, you dont write to same value at the same time from multiple threads. Also, that you don't read values from buffers, which may be written to at given time. Avoiding race conditions.

    Generally, I make sure, that I don't write to same buffer from different jobs at the same time. But is fine to read.

    Other than that, is dev's responsibility, to avoid conflicts.
     
    Opeth001 likes this.
  3. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    Not safe. Should only write to a buffer from the same thread (unless you are using it as an array and know for certain you won't be using the same index from different threads.)

    If you're using multiple jobs, don't schedule jobs at same time? Should have a dependency on each other.

    Real talk though, if you need to write to a buffer from multiple threads, rethink and refactor what you're trying to do. There is most likely a better way to do it.
     
    Opeth001 and Antypodish like this.
  4. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,112
    Thank you guys!