"are c# arrays thread safe?" Code Answer


i believe that if each thread only works on a separate part of the array, all will be well. if you're going to share data (i. e. communicate it between threads) then you'll need some sort of memory barrier to avoid memory model issues.

i believe that if you spawn a bunch of threads, each of which populates its own section of the array, then wait for all of those threads to finish using thread.join, that that will do enough in terms of barriers for you to be safe. i don't have any supporting documentation for that at the moment, mind you ...

edit: your sample code is safe. at no time are two threads accessing the same element - it's as if they each have separate variables. however, that doesn't tend to be useful on its own. at some point normally the threads will want to share state - one thread will want to read what another has written. otherwise there's no point in them writing into a shared array instead of into their own private variables. that's the point at which you need to be careful - the coordination between threads.

By Brandon McClure on July 25 2022

Answers related to “are c# arrays thread safe?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.