Qt与成员函数并发 [英] QtConcurrent with member function

查看:188
本文介绍了Qt与成员函数并发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I create a QFuture that I want to use to parallelize calls to a member function. More precisely, I have a class solveParallel with .h :

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

  void runCompute(solveModel * model_);

  bool resultComput();

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

其中方法runCompute()正在创建myFutureCompute成员. .cpp看起来像

where the methode runCompute() is creating the myFutureCompute member. .cpp looks like

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

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

Include(s) are all right. Compilation fails, on line

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"的鼠标信息上:错误:非静态成员引用必须相对于特定对象.

In addition, on mouse info for '&Manager' in same line of code stands: Error: a nonstatic member reference must be relative to a specific object.

您知道诀窍在哪里吗?谢谢和问候.

Do you see where is the trick? Thanks and regards.

推荐答案

来自官方文档:

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

QtConcurrent::run() also accepts pointers to member functions. The first argument must be either a const reference or a pointer to an instance of the class. Passing by const reference is useful when calling const member functions; passing by pointer is useful for calling non-const member functions that modify the instance.

您正在将指针传递给指针.还要注意,您不能像以前那样传递参数,而是将其作为run函数中的附加参数.以下应该可以工作:

You are passing a pointer to a pointer. Also notice that you cannot pass the arguments the way you do, but as extra arguments in the run function. The following should work:

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

这篇关于Qt与成员函数并发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆