Work with various std :: vector elements in parallel

Say I have one std::vector<Object*>. The vector is large (> 1000 elements), and everyone Object*should have extensive calculations performed on it. Thus, a for loop that starts each calculation for each element can be easily parallelized. In fact, I could handle all 1000 elements in parallel for maximum acceleration ("embarrassing in parallel?")

Now I am interested in 2 things:

1) Is it safe to read and write different items std::vectorwithout locking? ( not modifying the vector itself!)

2) Are there simple ways or conventions or patterns for cutting a for loop and sending it to threads?

+5
source share
4 answers

1) Yes

2) You can use OpenMP to parallelize vector processing. If you are working with Microsoft VC ++ 2010, the Concurrency library has parallel_for and parallel_for_each algorithms.

+4
source

For 1, see my answer here :

§ 23.2.2 Racing of these containers

2 / Despite (17.6.5.9), to prevent data races, an implementation is required when the contents of the contained object in different elements in the same sequence, except vector<bool>, change simultaneously.

, ++ 11 ( ++ 98/03 ), .

, . , OpenMP .

, , , , , , , "" , , parallelism .

+4
0

If you use g ++, you can use gnu parallel mode http://gcc.gnu.org/onlinedocs/libstdc++/manual/parallel_mode.html using standard stl algorithms in combination with lambdas that automatically parallelize.

0
source

All Articles