Answer the question
In order to leave comments, you need to log in
How to parallelize a loop using thread?
The len variable is always equal to 32, so I divide it into 4 threads, each of which is supposed to perform 8 calculations, but it turns out that the calculation time increases compared to one thread. What am I doing wrong?
The loop itself:
vector<thread> thds;
for (int i = 0; i < len; i = i+4) { // основной цикл умножения
thds.emplace_back(thread (multi, x, y, ref(res), len, i));
thds.emplace_back(thread(multi, x, y, ref(res), len, i+1));
thds.emplace_back(thread(multi, x, y, ref(res), len, i+2));
thds.emplace_back(thread(multi, x, y, ref(res), len, i+3));
thds[i].join();
thds[i+1].join();
thds[i+2].join();
thds[i+3].join();
//cout << i << endl;
}
void multi(vector<int>& x, vector<int>& y, vector<int>& res, int len, int i) {
for (auto j = 0; j < len; ++j)
res[i + j] += x[i] * y[j];
}
Answer the question
In order to leave comments, you need to log in
Why create threads every time in a loop? It is better to first create the required number of threads, taking into account their number in the system, and give the threads a range for calculations. Maybe you should take something ready-made like en.cppreference.com/w/cpp/thread/async see the example below, there is just a parallel summation of the vector elements. Note that if there are less than 1000 elements, no threads are created at all.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question