【问题标题】:QtConcurrent with member functionQtConcurrent 与成员函数
【发布时间】:2012-04-03 15:13:24
【问题描述】:

我创建了一个 QFuture,我想用它来并行化对成员函数的调用。更准确地说,我有一个 .h 类 solveParallel :

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

  void runCompute(solveModel * model_);

  bool resultComput();

private:
  Manager *myMgr;
  QFuture<bool> myFutureCompute;
}; 

方法 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();
}

包含都可以。编译失败,在线

futureComput = QtConcurrent::run(&this->myMgr,&Manager::compute(model));

出现此错误:

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

此外,在同一行代码中“&Manager”的鼠标信息代表:错误:非静态成员引用必须相对于特定对象。

你知道诀窍在哪里吗?谢谢和问候。

【问题讨论】:

    标签: c++ multithreading qt parallel-processing qtconcurrent


    【解决方案1】:

    来自official documentation

    QtConcurrent::run() 也接受指向成员函数的指针。这 第一个参数必须是 const 引用或指向 类的实例。通过 const 引用传递在以下情况下很有用 调用 const 成员函数;通过指针传递对 调用修改实例的非常量成员函数。

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

    futureComput = QtConcurrent::run(this->myMgr,&Manager::compute, model);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 2011-06-05
      • 1970-01-01
      相关资源
      最近更新 更多