Thread-safety of read-only memory access

前端 未结 2 1055
囚心锁ツ
囚心锁ツ 2021-01-06 04:11

I\'ve implemented the Barnes-Hut gravity algorithm in C as follows:

  1. Build a tree of clustered stars.
  2. For each star, traverse the tree and apply the gr
相关标签:
2条回答
  • 2021-01-06 05:08

    If your data is read-only, then no, you do not need to make a private copy of the tree for each thread. This is the biggest advantage that a shared memory threading model offers!

    I'm not aware of any performance problems with such a model. If anything, it should be faster depending on if your CPUs can share some of their cache.

    0 讨论(0)
  • 2021-01-06 05:09

    You don't specify how your data is structured, but in general reading memory from multiple threads simultaneously is safe and does not introduce any performance issues. You only get problems if someone is writing.

    It is interesting that you say you're only getting 30% speedup out of two threads. If you have an otherwise idle machine, two or more CPUs and only readonly shared data (i.e. no synchronization) I would expect to see much closer to 50% speed improvement. This suggests that your operation is actually completing so quickly that the overhead of creating the thread is becoming significant in your numbers. Are you running on a hyperthreaded CPU?

    0 讨论(0)
提交回复
热议问题