0

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?

1
  • 4
    No. If it is read-only, then there is no problem (of course given no other thread will write, etc.). One two writes or read-write combos will cause troubles. Commented Jul 30, 2017 at 13:15

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

It is the shared writes which cause problems. Multiple readers are always safe. Multiple writers if they are writing to non-overlapping ranges are also safe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.