0

I am trying to start a new thread everytime when ObjParser::loadData() is called like they did it in this example.

So I wrote this code.

#include <thread>   
void ObjParser::loadData()
{
   thread loadingThread(_loadData);
   loadingThread.detach();
}

void ObjParser::_loadData()
{
   //some code
}

But when I try to compile it I get this error:

error C3867: 'ObjParser::_loadData': function call missing argument list; use '&ObjParser::_loadData' to create a pointer to member

So I created a pointer to the member function:

#include <thread>   
void ObjParser::loadData()
{
   thread loadingThread(&ObjParser::_loadData);
   loadingThread.detach();
}

void ObjParser::_loadData()
{
   //some code
}

But then the compiler complaines:

error C2064: term does not evaluate to a function taking 0 arguments 

I have no ideas what causes the problem, could you please give me a hint how to solve this problem.

ThunderStorm
  • 766
  • 8
  • 24
  • You need to add an instance argument on which the member function will be called. See here http://stackoverflow.com/a/10673671/893693 .Note: Pay attention on the lifetime of the object if you don't copy it. – Stephan Dollberg Sep 03 '13 at 15:28
  • 1
    Are you sure you want to create a thread here? a detached thread? Could `std::async` be better fitted for your purpose? – David Rodríguez - dribeas Sep 03 '13 at 15:32
  • Are you sure thread detach is a good idea? – doctorlove Sep 03 '13 at 16:13
  • Thank you for your answers. Why is using detach() not a good idea? If I didn't use detach(), my action started by the thread object will be stopped, doesn't it? – ThunderStorm Sep 03 '13 at 17:57

2 Answers2

5

_loadData appears to be a non-static member, so you'll need to call it on an object - presumably, the same object that loadData was called on:

thread loadingThread(&ObjParser::_loadData, this);

or with a lambda

thread loadingThread([this]{_loadData();});

Alternatively, I might drop the extra function and just use a lambda:

thread loadingThread([this]{  // or [] if you don't need to access class members
    // some code
});
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

The C++ 11 threads work very simular to Boost's threads. They expect a function or function object that takes no arguments, so if your function is a member function of the class your working with, you will need to bind the function pointer to an instance of the object and any parameters that are needed. Look at std::bind std::mem_fn and such...

diverscuba23
  • 2,165
  • 18
  • 32
  • You're right that they work like boost threads, but wrong that they expect a 0-argument function or that you need to use bind, `boost::thread t(&SomeClass::memfunc, this, arg1, arg2)` has been supported for many, many years, and works with `std::thread` too – Jonathan Wakely Oct 09 '14 at 10:31
  • @Johnathan Wakely: Ok we are both arriving at the same type of construct via two different methods, the method I stated is explicit before the call, yours takes advantage of using initializer lists to first construct the std::function compatable object that std::thread expects. – diverscuba23 Oct 09 '14 at 18:04