samedi 1 juillet 2017

Error passing a reference of vector on a thread to an object's member function

I have 3 files: future.cpp, future.hpp and main.cpp. I have a future class declared in .hpp as follows:

class future_multithread{

private:
std::vector<std::vector<int>>my_input_list;
int number_of_cores;
int (*arbitrary_function)(std::vector<int>&);
std::vector<std::thread> thread_track;
std::vector<std::future<int>> futr;
semaphore* sem;

void control_threads(std::vector<int>&, std::promise<int>&&);
void launch_threads();

public:
future_multithread(int, std::vector<std::vector<int>>, int 
(*given_function)(std::vector<int>&));
void print();
void get_function_results(std::vector<int>&);
~future_multithread();

};

I am having issues in the void get_function_results(std::vector&) function. Its implementation is as follows in the .cpp:

void future_multithread::get_function_results(std::vector<int>& results){
launch_threads();
for_each(futr.begin(), futr.end(), [this, &results](std::future<int>& ft){
results.push_back(ft.get());
});
}

This function is called from the main.cpp as follows with obj being the object:

`

auto th = std::thread(&future_multithread::get_function_results, &obj, &result);
th.join();`

I have a vector in the main that needs to be filled by the future get() in this function. Since future get() code is blocking, I wanted to launch it on a thread so that my main can carry on till this result is updated, instead of blocking. It was working perfectly when I was returning a vector,earlier from this function. But it is failing now on a thread with passed reference.

The errors that I got are:

`error: cannot apply member pointer ‘((const std::_Mem_fn_base<void (future_multithread::*)(std::vector<int>&), true>*)this)->std::_Mem_fn_base<void (future_multithread::*)(std::vector<int>&), true>::_M_pmf’ to ‘* __ptr’, which is of non-class type ‘future_multithread*’
  { return ((*__ptr).*_M_pmf)(std::forward<_Args>(__args)...); }`

and this:

error: return-statement with a value, in function returning 'void' [-fpermissive]{ return ((*__ptr).*_M_pmf)(std::forward<_Args>(__args)...); }

I tried many things but I am unable to figure what is wrong. Any help is appreciated!

Aucun commentaire:

Enregistrer un commentaire