Fork me on GitHub

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

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

 

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

 

void getRegResultByOneSetpThread(const  int decodeType,   vector<vector<float>>& probAll, 
            const int m_roiBegin, const int m_roiEnd,const int topN, const cv::Mat& g_oriPicMat, string& regJson)

 


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指针才行,(普通成员函数在参数传递时编译器会隐藏地传递一个this指针.通过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

posted @ 2019-07-25 00:26  hellowOOOrld  阅读(1936)  评论(0编辑  收藏  举报