I am facing a strange issue when passing a vector std::ref to pool of threads.
I cannot put the exact code but I have something like : Approach A
void myfunc(int start, int end, std::vector<double>& result)
{
for(int i=start; i<end; ++i)
{
result[i] = ...
}
}
vector<double> myvec(N);
const size_t num_threads = std::thread::hardware_concurrency();
vector<std::thread> threads;
threads.reserve(num_threads);
for(int idx=0; idx<num_threads; ++idx)
threads.push_back(std::thread(myfunc, (idx*nloop/num_threads), (idx+1)*nloop/num_threads), std::ref(myvec)));
for(std::thread& t : threads)
{
if(t.joinable())
t.join();
}
some_read_opration(myvec)...
But if I explicitly create threads and divide tasks by hard coding ranges, it gives me expected values.
This works: Approach B
std::thread t1(myfunc, 0, nloop/2, std::ref(myvec));
std::thread t2(myfunc, nloop/2, nloop, std::ref(myvec));
t1.join();
t2.join();
some_read_opration(myvec)...
I want parallelize my loop in more generic way, not sure where I am going wrong here. In the A approach : The issue I am facing is that, it seems like each thread updates result[]
element locally but its not being reflected at myvec
in the function which creates threads, even if I am passing std::ref(myvec)
. Any help would be great.
Aucun commentaire:
Enregistrer un commentaire