Search Unity

Custom Native Containers - can we use locks, or just Interlocked atomics?

Discussion in 'Entity Component System' started by PeterJK, Dec 9, 2018.

  1. PeterJK

    PeterJK

    Joined:
    Dec 2, 2012
    Posts:
    35
    Hey everyone,

    I'm looking to create a custom native container, and am using the NativeCounter example as a starting point.

    I noted that it uses Interlocked atomics in order to handle concurrent access to the count value.

    Is it possible to use other methods for more complicated read/write patterns than increment/decrement/compare-exchange?

    I tried to use a simple lock, but the Burst compiler doesn't seem to like it - I guess because it depends on a reference type.

    Does anyone know if there's a tutorial or document anywhere outlining what concurrency tools we are able to use in a custom Native Container with Burst?

    Many thanks for any help :)
     
  2. PeterJK

    PeterJK

    Joined:
    Dec 2, 2012
    Posts:
    35
    To answer my own question - seems you can used Interlocked compare/exchange to do pretty arbitrary read/write operations on a value in a threadsafe and lock-free way:

    https://docs.microsoft.com/en-us/do...ocked.compareexchange?view=netframework-4.7.2

    I think that will do for my purposes! I guess if contention becomes a problem I could look at the PerThread counter example also as a basis for something more.
     
    Abbrew likes this.