0

Suppose there's

class concurr{
public:

double arr[100];
void func1(vector<vector<double>> data,int x);
double func2(vector<vector<double>> data);
}

such that func2 takes in "data" and feeds it to func1 with the same "data" but varying numbers of x as it goes from 0 to up to 100. Then in func1, it calculates something and whatever the double value it comes out, fills it up for arr[x] So basically, the func2 looks generally like

    double concurr::func2(vector<vector<double>> data){

        thread threads[100];
        for (int x = 0; x < 100; x++){
            threads[x] = thread(concurr::func1,data,x); // this line is where the problem is
        }
        for (auto& th : threads){
            th.join();
        }
        // ..... does something with the arr[100].... to come up with double = value
        return value;
    }

and without the multithreading part, the code runs well, it's just that with the added multithreading part, when I try to compile, it says "reference to non-static member function must be called"

Mike Chen
  • 35
  • 4
  • You can't throw a bare member function at a `thread` and expect it to know what you're talking about. These functions need to be called with an object in mind. You could use a lambda instead of a bare function. – tadman Mar 05 '18 at 17:59
  • You should either change `func1` to a static function, or pass the `concurr` object (that `func1` should be called on) to `thread()`. – frslm Mar 05 '18 at 18:00

1 Answers1

3

When calling member function with a thread you need to pass an object of the class or a ref to it (in your case itself):

threads[x] = thread(concurr::func1, *this,data,x);

Or use a lambda

threads[x] = thread( [&]{ this->func1(data, x); } )
HangrY
  • 139
  • 8