Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QtConcurrent with member function

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

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

with this error:

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

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.

like image 323
kiriloff Avatar asked Apr 03 '12 15:04

kiriloff


People also ask

How do you call a member function in Qt concurrent?

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.

What is the difference between qfuture and qtconcurrent?

Note that the QFuture::result () function blocks and waits for the result to become available. Use QFutureWatcher to get notification when the function has finished execution and the result is available. QtConcurrent::run () also accepts pointers to member functions.

What is run () function in qtconcurrent?

In basic mode, the function passed to QtConcurrent::run () is able to report merely a single computation result to its caller.

Can I call overloaded functions in qtconcurrent?

Note that QtConcurrent::run does not support calling overloaded functions directly. For example, the code below won't compile: void foo(int arg) ; void foo(int arg1, int arg2) ; ...


1 Answers

From the official documentation :

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.

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);
like image 131
pnezis Avatar answered Oct 16 '22 11:10

pnezis