[英]C++ 11 : Start thread with member function and this as parameter
使用此代码,我得到了错误:
错误1错误C2064:term不评估为带有1个参数的函数c:\\ program files(x86)\\ microsoft visual studio 11.0 \\ vc \\ include \\ functional 1152 1 Pipeline
class PipelineJob {
private:
std::thread *thread;
void execute(PipelineJob* object);
public:
void execute(PipelineJob* object)
{
}
PipelineJob()
{
this->thread = new std::thread(&PipelineJob::execute, this);
}
};
我尝试了很多变化,现在任何一个如何解决这个问题?
为简单起见,删除模板和指针,这或多或少是您想要的:
class PipelineJob
{
private:
std::thread thread_;
void execute(PipelineJob* object) { ..... }
public:
PipelineJob()
{
thread_ = std::thread(&PipelineJob::execute, this, this);
}
~PipelineJob() { thread_.join(); }
};
请注意, this
会将两次传递给std::thread
构造函数:一次用于成员函数的隐式第一个参数,第二个用于成员函数的可见参数PipelineJob* object
。
如果你的execute
成员函数不需要外部PipelineJob
指针,那么你需要类似的东西
class PipelineJob
{
private:
std::thread thread_;
void execute() { ..... }
public:
PipelineJob()
{
thread_ = std::thread(&PipelineJob::execute, this);
}
~PipelineJob() { thread_.join(); }
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.