小编Moo*_*oks的帖子

C++ 11:在构造函数中使用线程初始化执行函数成员的类中的std :: thread

我正在尝试使用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 …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading stl c++11

14
推荐指数
2
解决办法
2万
查看次数

标签 统计

c++ ×1

c++11 ×1

multithreading ×1

stl ×1