【发布时间】:2011-08-22 20:11:19
【问题描述】:
我正在尝试使用 C++11 中的 std::thread。如果可以在执行其函数成员之一的类中拥有 std::thread ,我无法找到任何地方。考虑下面的例子...... 在我的尝试中(如下),函数是 run()。
我使用带有 -std=c++0x 标志的 gcc-4.4 进行编译。
#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’|
【问题讨论】:
-
您可能希望在 stop() 函数中将 m_stop 设置为 true ....
标签: c++ multithreading stl c++11