I have 3 files: future.cpp, future.hpp and main.cpp. I have a future class declared in .hpp as follows:
我有3个文件:future.cpp,future.hpp和main.cpp。我有一个在.hpp中声明的未来类,如下所示:
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 get_function_results(std :: vector&)函数中遇到问题。它的实现如下.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:
从main.cpp调用此函数,如下所示:obj是对象:
`
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.
我在main中有一个向量,需要在此函数中由未来的get()填充。由于将来的get()代码是阻塞的,我想在一个线程上启动它,以便我的main可以继续直到这个结果被更新,而不是阻塞。当我在此函数之前返回向量时,它工作得很好。但是现在在传递引用的线程上失败了。
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!
我尝试了很多东西,但我无法弄清楚是什么问题。任何帮助表示赞赏!
1 个解决方案
#1
0
Unfortunately you haven't given context for std::thread constructor call, so I don't know of what type obj
and result
are. I assume that obj
is of future_multithread
type and result
of std::vector<int>
. In this cases the error is that you are passing pointer to vector instead of a reference: you should change &result
to result
.
不幸的是你还没有为std :: thread构造函数调用提供上下文,所以我不知道obj和result是什么类型的。我假设obj是future_multithread类型和std :: vector
#1
0
Unfortunately you haven't given context for std::thread constructor call, so I don't know of what type obj
and result
are. I assume that obj
is of future_multithread
type and result
of std::vector<int>
. In this cases the error is that you are passing pointer to vector instead of a reference: you should change &result
to result
.
不幸的是你还没有为std :: thread构造函数调用提供上下文,所以我不知道obj和result是什么类型的。我假设obj是future_multithread类型和std :: vector