类内成员函数 多线程调用 调试记录

 最近工程上需要用到多线程调用类内成员函数,记录一下当时出错的问题,及解决方法。

1.首先 写法是普通多线程调用时候的声明,如下:


std::thread t0(getRegResultByOneSetpThread, 0, probAll, m_roiBegin, m_roiEnd, topN, g_oriPicMat,regAll);

结果会报如下错误:

error: invalid use of non-static member function

2.然后查找资料,得知类内成员函数多线程调用时需要声明为static形式,或者传入this指针才行,

Agent_Classifier 为类名。

修改为如下形式:


std::thread t0(&Agent_Classifier::getRegResultByOneSetpThread,this 0, probAll, m_roiBegin, m_roiEnd, topN, g_oriPicMat,regAll);

结果会报如下错误:

error: no type named ‘type’ in ‘class std::result_of

3.原因是 线程函数 getRegResultByOneSetpThread 参数类型有引用类型,必须传入引用类型才可以:

std::thread t0(&Agent_Classifier::getRegResultByOneSetpThread,this, 0, std::ref(probAll), m_roiBegin,  m_roiEnd, topN,  std::ref(g_oriPicMat),   std::ref(regAll));

 至此编译通过。

参考:

https://stackoverflow.com/questions/41476077/thread-error-invalid-use-of-non-static-member-function

https://stackoverflow.com/questions/28950835/c-error-no-type-named-type-in-class-stdresult-ofvoid-stdunordered

猜你喜欢

转载自www.cnblogs.com/hellowooorld/p/11233529.html
今日推荐