0

So, I'm kinda new to parallel computing...

Let's suppose I have an array arr in C++ code. Does accessing cells with different indexes from different threads create a race condition? For example if one thread will set some value let's say to arr[i] and the second one will write into arr[j] ( where i != j ).

user3666197
  • 1
  • 6
  • 50
  • 92
  • 1
    There is no inherent race condition, the semantics are well defined. Having said that, there's an entire set of rules as to exactly how one thread 'sees' any changes made by other execution threads. There's no issue, in your example, of one thread setting `arr[i]` at the same time another thread sets `arr[j]`. However, that in itself makes no guarantees as to when and where another execution thread sees the new values in `arr[i]` and `arr[j]` (or these two threads see each others' change). That is a completely different subject. A very, very complicated, and convoluted, subject... – Sam Varshavchik Jun 14 '20 at 22:10
  • Note that while access to disjoint subsets of the array (or vector) won't introduce a race condition, pushing/popping/resizing the vector definitely would. – Jeremy Friesner Jun 15 '20 at 04:25

1 Answers1

1

If you make sure that all threads use the same array instance and that every thread uses its own index, then there will be no race.

mentallurg
  • 4,967
  • 5
  • 28
  • 36