Possible Duplicate:
Start thread with member function
I'm VERY new to C++. My experience has mostly been with javascript and java.
I'm using Xcode on Lion. The following code gives me a compilation error "Reference to non-static member function must be called; did you mean to call it with no arguments?"
class MyClass {
private:
void handler() {
}
public:
void handleThings() {
std::thread myThread(handler);
}
};
I also tried this->handler, &handler, and other variations, but none of them worked. This code compiles though and accomplishes what I want it to:
class MyClass {
private:
void handler() {
}
public:
void handleThings() {
std::thread myThread([this]() {
handler();
});
}
};
Why can't I pass a reference to a member function? Is my work-around the best solution?
std::bindinstead of lambda. to call a method of a class you have to provide an instance of that class (thispointer as first implicit parameter of any non static member-function). passing just an address of the method you want to call (w/o the instance) tostd::threadctor is not enough obviously.std::asyncinstead ofstd::thread... in case if running thread just calc smth and exits (i.e. not an even loop handler)