QtConcurrent与成员函数

kir*_*off 5 c++ parallel-processing qt multithreading qtconcurrent

我创建了一个QFuture,我想用它来并行调用成员函数.更确切地说,我有一个与.h一起求解的类:

class solverParallel {
public:
  solverParallelData(Manager* mgr_);
  virtual ~solverParallel(void);

  void runCompute(solveModel * model_);

  bool resultComput();

private:
  Manager *myMgr;
  QFuture<bool> myFutureCompute;
}; 
Run Code Online (Sandbox Code Playgroud)

方法runCompute()在哪里创建myFutureCompute成员..cpp看起来像

solveParallel::solveParallel(Manager* mgr_)
:m_mgr(mgr_)
{
}

solverParallel::~solverParallel(void){}

void solverParallel::runCompute(solveModel* model)
{
  futureComput = QtConcurrent::run(&this->myMgr,&Manager::compute(model));
}

bool solverParallelData::resultComput()
{
  return m_futureComput.result();
}
Run Code Online (Sandbox Code Playgroud)

包括在内是可以的.编译失败,在线

futureComput = QtConcurrent::run(&this->myMgr,&Manager::compute(model));
Run Code Online (Sandbox Code Playgroud)

有这个错误:

Error   44  error C2784: 'QFuture<T> QtConcurrent::run(T (__cdecl *)(Param1),const     Arg1 &)' : could not deduce template argument for 'T (__cdecl *)    (Param1)' from 'Manager **'   solverparallel.cpp 31
Run Code Online (Sandbox Code Playgroud)

此外,在同一行代码中的'&Manager'的鼠标信息上:缺席:非静态成员引用必须相对于特定对象.

你看到诀窍在哪里?感谢致敬.

pne*_*zis 14

官方文档:

QtConcurrent :: run()也接受指向成员函数的指针.第一个参数必须是const引用或指向该类实例的指针.在调用const成员函数时,通过const引用传递是很有用的.传递指针对于调用修改实例的非const成员函数很有用.

您正在指向指针.另请注意,您不能按照您的方式传递参数,而是作为函数中的额外参数传递run.以下应该有效:

futureComput = QtConcurrent::run(this->myMgr,&Manager::compute, model);
Run Code Online (Sandbox Code Playgroud)