3 c++ multithreading c++11 stdthread
使用此代码,我得到了错误:
错误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);
}
};
Run Code Online (Sandbox Code Playgroud)
我尝试了很多变化,现在任何一个如何解决这个问题?
jua*_*nza 16
为简单起见,删除模板和指针,这或多或少是您想要的:
class PipelineJob
{
private:
std::thread thread_;
void execute(PipelineJob* object) { ..... }
public:
PipelineJob()
{
thread_ = std::thread(&PipelineJob::execute, this, this);
}
~PipelineJob() { thread_.join(); }
};
Run Code Online (Sandbox Code Playgroud)
注意,它this被传递两次到std::thread构造函数:一次用于成员函数的隐式第一个参数,第二个用于PipelineJob* object成员函数的visible参数.
如果你的execute成员函数不需要外部PipelineJob指针,那么你需要类似的东西
class PipelineJob
{
private:
std::thread thread_;
void execute() { ..... }
public:
PipelineJob()
{
thread_ = std::thread(&PipelineJob::execute, this);
}
~PipelineJob() { thread_.join(); }
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19572 次 |
| 最近记录: |