How to “multithread” C code

后端 未结 12 1755
暗喜
暗喜 2020-12-02 09:30

I have a number crunching application written in C. It is kind of a main loop that for each value calls, for increasing values of \"i\", a function that performs some calcul

12条回答
  •  情深已故
    2020-12-02 09:48

    If an iteration in loop is independent of the ones before it, then there's a very simple approach: try multi-processing, rather than multi-threading.

    Say you have 2 cores and ntimes is 100, then 100/2=50, so create 2 versions of the program where the first iterates from 0 to 49, the other from 50 to 99. Run them both, your cores should be kept quite busy.

    This is a very simplistic approach, yet you don't have to mess with thread creation, synchronization, etc

提交回复
热议问题