I'm currently trying to make my first steps in writing multi-threaded code in CPP and I'm getting a error message that I don't understand at all....
Consider the two functions:
void add_vector(std::vector<int> & vec, int & store){
store = std::accumulate(vec.begin(), vec.end(), 0);
}
int parallel_matrix_sum(std::vector<std::vector<int>> const & mat){
std::vector<std::thread> threads;
std::vector<int> tmp(l);
for (int k =0; k<l; k++){
threads.push_back(std::thread(add_vector, std::cref(mat[k]), std::ref(tmp[k])));
}
for(auto && thread: threads){
thread.join();
}
return std::accumulate(tmp.begin(), tmp.end(), 0);
}
The error I get is
error: attempt to use a deleted function
.
.
.
in instantiation of function template specialization 'std::thread::thread<void (&)(std::vector<int> &, int &), std::reference_wrapper<const std::vector<int>>, std::reference_wrapper<int>, void>' requested here
threads.push_back(std::thread(add_vector, std::cref(mat[k]), std::ref(tmp[k])));
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/type_traits:1916:5: note: '~__nat' has been explicitly marked deleted here
~__nat() = delete;
Upon googling this issue, all the information points to the fact that std::thread
needs to copy the arguments, which cannot be done for references.. The suggested solution seems to be to wrap the arguments in a std::ref
call. I am already doing this and it is still not working. Does anybody have some suggestions?