I’m trying to parallelize the following function (pseudocode):
vector<int32> out;
for (int32 i = 0; i < 10; ++i)
{
int32 result = multiplyStuffByTwo(i);
// Push to results
out.push_back(result);
}
When I now parallelize the for loop and define the push_back part as a critical path, I’m encountering the problem that (of course) the order of the results in out is not always right. How can I make the threads run execute the code in the right order in the last line of the for loop? Thanks!
You can set the size of the out-vector by calling out.resize() and then set the value by index, not by push_back()
Pseudo-code:
But, I’d recommend using “classic” arrays. They’re much faster and not really harder to manage