Moo*_*oks 14 c++ multithreading stl c++11
我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)
我收到这个错误和其他人:(有和没有$ this相同的错误)
Runnable.h|9|error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, Runnable* const)’|
Run Code Online (Sandbox Code Playgroud)
传递指针时.
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’|
Run Code Online (Sandbox Code Playgroud)
ild*_*arn 17
这里有一些代码要仔细考虑:
#ifndef RUNNABLE_H
#define RUNNABLE_H
#include <atomic>
#include <thread>
class Runnable
{
public:
Runnable() : m_stop(), m_thread() { }
virtual ~Runnable() { try { stop(); } catch(...) { /*??*/ } }
Runnable(Runnable const&) = delete;
Runnable& operator =(Runnable const&) = delete;
void stop() { m_stop = true; m_thread.join(); }
void start() { m_thread = std::thread(&Runnable::run, this); }
protected:
virtual void run() = 0;
std::atomic<bool> m_stop;
private:
std::thread m_thread;
};
class myThread : public Runnable
{
protected:
void run() { while (!m_stop) { /* do something... */ }; }
};
#endif // RUNNABLE_H
Run Code Online (Sandbox Code Playgroud)
一些说明:
m_stop一样简单地宣称bool是非常不足够的; 阅读内存障碍std::thread::join可以扔掉所以在没有try..catch析构函数的情况下调用它是鲁莽的std::thread并且std::atomic<>是不可复制的,所以Runnable应该这样标记,如果没有其他原因而不是用VC++避免C4512警告Dav*_*eas 15
这种做法是错误的.
问题是,当对象仍在构建时,它的类型仍然不是最派生的类型,而是正在执行的构造函数的类型.这意味着当你启动线程时,对象仍然是一个,Runnable并且run()可以调用to Runnable::run(),这是纯虚拟的,这反过来会导致未定义的行为.
更糟糕的是,您可能会遇到虚假的安全感,因为在某些情况下,正在启动的线程可能需要足够长的时间才能使当前线程完成Runnable构造函数,并输入myThread对象,在这种情况下新线程将执行正确的方法,但更改执行程序的系统(不同的核心数,或系统的负载,或任何其他不相关的情况),程序将在生产中崩溃.
| 归档时间: |
|
| 查看次数: |
24497 次 |
| 最近记录: |