Reputation: 67254
Say I have a std::vector<Object*>
. The vector is large ( > 1000 elements) and each Object*
needs to have extensive computation done on it. The for loop that runs each the computation on each element then, can be easily parallelized. In fact, I could process all 1000 elements in parallel for maximum speedup ("embarrassingly parallel?")
Now I'm wondering 2 things:
1) Is it safe to read and write to different elements of a std::vector
without a lock? (not modifying the vector itself!)
2) Are there easy ways or conventions or patterns to follow to cut a for loop and dispatch to threads?
Upvotes: 6
Views: 3643
Reputation: 97
If you are using g++, you can use gnu parallel mode http://gcc.gnu.org/onlinedocs/libstdc++/manual/parallel_mode.html using the standard stl algorithms in combination with lambda's which are auto parallelized.
Upvotes: 0
Reputation: 43321
1) Yes
2) You can use OpenMP to paralellize vector handling. If you are working with Microsoft VC++ 2010, Concurrency library has parallel_for and parallel_for_each algorithms.
Upvotes: 4
Reputation: 299930
For 1, see my answer here:
§ 23.2.2 Container data races
2/ Notwithstanding (17.6.5.9), implementations are required to avoid data races when the contents of the contained object in different elements in the same sequence, excepting
vector<bool>
, are modified concurrently.
Obviously this is C++11 (as C++98/03 made no mention of threads), however most implementations already comply.
I am afraid I don't know any convention. OpenMP could probably automate this.
To do it manually, I would probably use a queue-like system, as not all objects may require the same time, you could have one thread finish well after the others if you split in "equal" portions, whereas a queue feeding the threads would alleviate this issue by maximizing the parallelism up until the very end.
Upvotes: 4
Reputation: 17428
If you are using VS 2010 you can use the Parallel Patterns library to achieve the multi-threading.
Upvotes: 0