I am trying to build the program that I am using and got the following question. Will I get a performance loss if multiple threads have to read / write on the same vector, but different elements of the vector? I have the feeling that my program is having difficulty coping with this. Take the following code:
#include <vector> int main(){ vector<double> numbers; vector<double> results(10); double x; //write 10 values in vector numbers for (int i =0; i<10; i++){ numbers.push_back(cos(i)); }
Obviously, the actual program performs much more expensive operations, but this example should only explain my question. Thus, a for loop can be executed quickly and completely in parallel, or should different threads wait for each other, because only one thread at a time can access the vector number, for example, although they all read different elements of the vector?
The same question with the write operation: do I need a critical pragma, or is this not a problem, since each stream writes to another element of vector results? I am happy with every help I can get, and it would also be nice to know if there is a better way to do this (maybe not using vectors at all, but simple arrays and pointers, etc.?) I also read that the vectors in In some cases, they are not thread safe, and it is recommended to use a pointer: OpenMP and STL vector
Many thanks for your help!
source share