Alex
Alex

Reputation: 3968

C# Array 2 threads read same item at same time

If I have the following 2 for loops that will be run on different threads:

for (int ii = 1; ii < times.Length; ii+=2)
{
     if (times[ii] - times[ii - 1] > maxGap)
     return false;
}

for (int ii = 2; ii < times.Length; ii += 2)
{
      if (times[ii] - times[ii - 1] > maxGap)
      return false;
}

The situation could occur where both threads try to read from an item in times Array eg both threads could try and read times[1] at the same time.

Given that both threads will only ever read from these arrays, is this a problem eg could it cause my code to crash or any other unexpected negative consequences?

Upvotes: 0

Views: 492

Answers (1)

Chandrasekhar Raman
Chandrasekhar Raman

Reputation: 696

If only read operation is performed then there won't be a problem. Read and write operation together can cause a problem.

Upvotes: 4

Related Questions