Я пытаюсь использовать std :: thread из C ++ 11. Я нигде не мог найти, возможно ли иметь std :: thread внутри класса, выполняющего один из его членов-функций. Рассмотрим пример ниже ... В моей попытке (ниже) функция run ().
Я компилирую с gcc-4.4 с флагом -std = c ++ 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’|