I am trying to execute the following code and I keep getting an Index out of range exception when trying to assign array values to the list:-
int[] array = new int[1000000];
for (int i = 0; i < array.Length; i++)
{
array[i] = i;
}
List<int> list = new List<int>();
Parallel.For(0, array.Length, i => list.Add(array[i]));
Am I doing something wrong here ? I understand that the process is unordered/asynchronous, but why does “i” get values that are higher than the value of “array.Length” ?
The problem is that you cannot call
List.Add()concurrently on multiple threads. If you need thread-safe collections, see theSystem.Collections.Concurrentnamespace.If you break into the debugger when you get an exception, you’ll see that
iis not greater thanarray.Length, but is instead a power of 2 that is substantially less thanarray.Length. What happens is that theListstarts out with an empty array of something like 4 elements. Whenever you add an element to a list whose array is full, it creates an array of twice the length of the old array, copies the old elements to it, and stores the new array.Now let’s say that your list is up to 31 elements (meaning it has space for one more) and two threads try to add a 32nd element. They will both execute code like this:
First they will both see that
_size(31) is not_items.Length(32), so they both execute_size++. The first thread will get 31 (the correct index of the 32nd element) and change_sizeto 32. The second thread will get 32 and try to index_items[32], which gives you your exception because it’s trying to access the 33rd element of a 32-element array.