For the code snippet below, I keep getting the invoke error C6272. I have tried multiple things - passing using ref, without it and even testing as a simple thread. For context, the member function is a function that multiplies two sparse matrices and adds them to a linked list. Without using threads, the function works fine but threads returns an error.
mutex m;
vector<thread> a;
for (int q = 0; q < rhs.num_columns_; q ) {
a.push_back(thread(&SparseMatrix::mul_node, rhs_rows, lhs_rows, q, ref(newMatrix), ref(m)));
}
for (thread& t : a) {
t.join();
}
Declaration of the mul_node function
void SparseMatrix::mul_node(vector<vector<int>> rhs, vector<vector<int>> lhs, int pos_rhs, row_node* &newMatrix, mutex &m) const`
I have not been able to find a solution yet for the problem above, please let me know what exactly is causing the issue and how I can fix it? Thank you
CodePudding user response:
Since the member function is not static
you need to pass a pointer to the instance of the SparseMatrix
on to the std::thread
constructor too.
Simplified example:
#include <iostream>
#include <thread>
struct foo {
~foo() {
if(th.joinable()) th.join();
}
void run() {
th = std::thread(&foo::thread_func, this, 10, 20);
// ^^^^
}
void thread_func(int a, int b) {
std::cout << "doing the stuff " << a << ' ' << b << '\n';
}
std::thread th;
};
int main() {
foo f;
f.run();
}
Here 10
and 20
are passed as parameters to this->thread_func
.