Thread safety in C# arrays
Asked Answered
O

3

13

Does having 2 different threads :

  • one reading from a C# array (e.g from first location),
  • and another one writing to the same C# array but to a different location(e.g to the last location)

is thread safe or not?
(And I mean here without locking reading nor writing)

Odoriferous answered 9/5, 2010 at 19:38 Comment(0)
E
8

This particular case is safe, yes.

Reading and writing to different parts of an array does not interfere with the other operations.

However, reading and writing to the same location can give you problems, depending on the type of element, and the size of the elements.

Eared answered 9/5, 2010 at 19:41 Comment(4)
As long as those items are in seperate CPU wordsTitanesque
+1 Never thought this be threadsafe, but I understand why it is. Thanks!Johnajohnath
It is important to consider when using lock free algorithms that what you think of as "the same position" is not the same thing as what the CPU(s) think of as the same position. The CPUs view of memory is very different from the view languages like C give us where bytes occupy addresses. On some CPU architectures (notably ARM, Power) on some data types even this may not be safeTitanesque
That is correct, I should've made it clearer. I implicitly assumed it was a rather big array, so first and last item should then be safe, but yes, items together in memory might pose a problem.Eared
T
7

I'm not sure this is guaranteed to be safe. Imagine you have byte[]. Those bytes are closely packed in memory. Now, if you modify those bytes the compiler may coalesce some of the writes to perform word (32-bit) sized read modify write operations. On some CPUs, ARM for example, this is the only kind of memory modifying instruction the compiler has. This is especially handy if you are modifying more than one byte at a time. The CPU can do the same thing too. It can also reorder stuff without you knowing about it. In the face of that kind of optimization it is possible for a thread reading adjacent memory to see partial modifications. You don't see this kind of effect normally because the heap allocator is nice to you and always gives you memory which is at least word aligned.

Titanesque answered 9/5, 2010 at 19:54 Comment(1)
+1: In my opinion this is the best answer. It is always best to be skeptical of lock free threading idioms. I can think of all kinds of memory barrier problems that could cause problems even if the threads were working with different array positions. There just simply is not enough information provided in the question to give a definitive answer.Douceur
G
6

Long story short: Yes. As long as its to two different locations, its a safe operation.

There was a discussion about this awhile ago, it has some useful information if you're curious.

Goofball answered 9/5, 2010 at 19:42 Comment(1)
+1 Never thought this be threadsafe, but I understand why it is. Thanks!Johnajohnath

© 2022 - 2024 — McMap. All rights reserved.