Dividing loop iterations among threads

后端 未结 8 544
广开言路
广开言路 2021-01-05 03:41

I recently wrote a small number-crunching program that basically loops over an N-dimensional grid and performs some calculation at each point.

for (int i1 =          


        
相关标签:
8条回答
  • 2021-01-05 04:39

    The first approach is enough. No need for complication here. If you start playing with mutexes you risk making hard to detect errors.

    Don't start complicating unless you really see that you need this. Syncronization issues (especially in case of many threads instead of many processes) can be really painful.

    0 讨论(0)
  • 2021-01-05 04:45

    If you want to write multithreaded number crunching code (and you are going to be doing a lot of it in the future) I would suggest you take a look at using a functional language like OCaml or Haskell.

    Due to the lack of side effects and lack of shared state in functional languages (well, mostly) making your code run across multiple threads is a LOT easier. Plus, you'll probably find that you end up with a lot less code.

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