Я пытаюсь использовать std:: thread из С++ 11. Я не мог найти нигде, если возможно иметь std:: thread внутри класса, выполняющего один из его членов функции. Рассмотрим пример ниже... В моей попытке (ниже) функция запускается().
Я компилирую с gcc-4.4 с флагом -std = С++ 0x.
#ifndef RUNNABLE_H
#define RUNNABLE_H
#include <thread>
class Runnable
{
public:
Runnable() : m_stop(false) {m_thread = std::thread(Runnable::run,this); }
virtual ~Runnable() { stop(); }
void stop() { m_stop = false; m_thread.join(); }
protected:
virtual void run() = 0;
bool m_stop;
private:
std::thread m_thread;
};
class myThread : public Runnable{
protected:
void run() { while(!m_stop){ /* do something... */ }; }
};
#endif // RUNNABLE_H
Я получаю эту ошибку и другие: (такая же ошибка с и без $this)
Runnable.h|9|error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, Runnable* const)’|
При передаче указателя.
Runnable.h|9|error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say ‘&Runnable::run’|