0

I have a vector of entities. At update cycle I iterate through vector and update each entity: read it's position, calculate current speed, write updated position. Also, during updating process I can change some other objects in other part of program, but each that object related only to current entity and other entities will not touch that object.

So, I want to run this code in threads. I separate vector into few chunks and update each chunk in different threads. As I see, threads are fully independent. Each thread on each iteration works with independent memory regions and doesn't affect other threads work.

Do I need any locks here? I assume, that everything should work without any mutexes, etc. Am I right?

Davide Spataro
  • 7,319
  • 1
  • 24
  • 36
Robotex
  • 1,064
  • 6
  • 17
  • 41
  • Re, "update an object in a thread," and "I want to run this code in threads." Your code always was run in _a_ thread. The smallest number of threads that any program can have is 1: That's the thread that calls `main(...)`. – Solomon Slow Nov 06 '19 at 18:10

2 Answers2

1

Short answer

No, you do not need any lock or synchronization mechanism as your problem appear to be a embarrassingly parallel task.

Longer answer

A race conditions that can only appear if two threads might access the same memory at the same time and at least one of the access is a write operation. If your program exposes this characteristic, then you need to make sure that threads access the memory in an ordered fashion. One way to do it is by using locks (it is not the only one though). Otherwise the result is UB. It seems that you found a way to split the work among your threads s.t. each thread can work independently from the others. This is the best case scenario for concurrent programming as it does not require any synchronization. The complexity of the code is dramatically decreased and usually speedup will jump up.

Please note that as @acelent pointed out in the comment section, if you need changes made by one thread to be visible in another thread, then you might need some sort of synchronization due to the fact that depending on the memory model and on the HW changes made in one thread might not be immediately visible in the other.

This means that you might write from Thread 1 to a variable and after some time read the same memory from Thread 2 and still not being able to see the write made by Thread 1.

Davide Spataro
  • 7,319
  • 1
  • 24
  • 36
  • You're ignoring the memory model completely. Unless an object is always handled by exactly the same thread, you still need synchronization, even if only one thread at a time is handling the object. Synchronization is the only guarantee that the changes made by the current thread are visible to other threads. – acelent Nov 06 '19 at 18:52
  • That is correct but I think it is clear from my answer that you do not need any synchronization as soon as you only read from the memory. You are talking about changes visibility which implies a write operation at some point. I will update the answer with your comment. Thanks. – Davide Spataro Nov 07 '19 at 08:40
0

"I separate vector into few chunks and update each chunk in different threads" - in this case you do not need any lock or synchronization mechanism, however, the system performance might degrade considerably due to false sharing depending on how the chunks are allocated to threads. Note that the compiler may eliminate false sharing using thread-private temporal variables.

You can find plenty of information in books and wiki. Here is some info https://software.intel.com/en-us/articles/avoiding-and-identifying-false-sharing-among-threads Also there is a stackoverflow post here does false sharing occur when data is read in openmp?

nae9on
  • 249
  • 3
  • 9