Suppose I have this scenario:
class Container {
public string Item1 { get; set; }
public int Item2 { get; set; }
}
// And I have an array like this:
List<Task> tasks = new List<Task>();
Container[] arr = new Container[10];
for (int i = 0; i < arr.Length; ++i) {
arr[i] = new Container();
tasks.Add(Task.Run(() => FillContainer(arr[i])));
}
Task.WaitAll(tasks.ToArray());
// Do something with arr like writing to json file
FillContainer mutates the container that is passed in (changes the Item1
and Item2
properties).
Would this be safe (assuming only that single block of code is being executed)?
I'm assuming it would be because each Container
object has it's own chunk of memory which is being mutated by a single thread.