I am planning to share a List between multiple threads. The list will be locked during a changes, which happen infrequently. Is there a thread safety issue if multiple iterations are made from different threads through the list simultaneously?
In fact, by default, classes are not thread-safe. Being thread-safe would mean that any operation modifying the list would need to be interlocked against simultaneous access. This would be necessary even for those lists that will only ever be used by a single thread. That would be very inefficient.
Only one thread can execute a method or block of code protected by the same object reference.
Yes, List<T> is fine to read from multiple threads concurrently, so long as nothing's writing.
The answer is no. Each array element has a region of memory reserved for it alone within the region attributed the overall array.
List<T>
is not thread-safe generally. Having multiple readers will not cause any issues, however, you cannot write to the list while it is being read. So you would need to lock on both read and write or use something like a System.Threading.ReaderWriterLock
(which allows multiple readers but only one writer).
It can be read from multiple threads simultaneously, if that's what you're asking. Consider a reader-writer lock if so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With