Thread-safety of read-only memory access

前端 未结 2 1064
囚心锁ツ
囚心锁ツ 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: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?

提交回复
热议问题