Pthread模板参数错误
Pthread template argument error
我正在尝试使用pthreads编写一个简单的parallel_fo。原始代码是这样的。
#ifdef HAVE_TBB
template<typename Body> static inline
void parallel_for( const BlockedRange& range, const Body& body )
{
tbb::parallel_for(range, body);
}
#else
template<typename Body> static inline
void parallel_for( const BlockedRange& range, const Body& body )
{
body(range);
}
#endif
我为TBB不存在的情况重写。这是我写的。我在编译它时遇到问题。threadFunc在我之前写template<typename Body>
时会出错。
In function 'void cv::parallel_for(const cv::BlockedRange&, const Body&)':
error: no matches converting function 'threadFunc' to type 'void* (*)(void*)'
error: candidates are: template<class Body> void* cv::threadFunc(void*)
这些是我收到的错误信息。可能会有一个简单的解决方案。谢谢
struct MyStruct {
BlockedRange range;
Body& body;
};
//template<typename Body>
static void* threadFunc(void *arg) {
MyStruct<Body> *rangeBody = (MyStruct<Body>*) arg;
rangeBody->body(rangeBody->range);
}
template<typename Body> static inline
void parallel_for( const BlockedRange& range, const Body& body )
{
BlockedRange& range1 = BlockedRange(range.begin(), range.end()/2);
BlockedRange& range2 = BlockedRange(range.end()/2, range.end());
MyStruct<Body> rangeBody1;
rangeBody1.range = range1;
rangeBody1.body = body;
MyStruct<Body> rangeBody2;
rangeBody2.range = range2;
rangeBody2.body = body;
pthread_t thread1, thread2;
pthread_create(&thread1, NULL, threadFunc, (void*) &rangeBody1);
pthread_create(&thread2, NULL, threadFunc, (void*) &rangeBody2);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
}
如果threadFunc
是一个函数模板,那么您需要专门化它以获得所需的函数:
pthread_create(&thread1, NULL, threadFunc<Body>, &rangeBody1);
^^^^^^
相关文章:
- 如何通过参数抛出错误消息
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 错误 没有与参数列表匹配的重载函数"getline"实例
- 为什么 std::绑定错误参数可以成功?
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- g++ 为 transform() 的参数生成错误
- 如何在C++中使用 Void 而不会收到有关参数的错误
- 错误:参数中无效地使用了无效表达式
- xgboost 构建错误:参数包未扩展为"..":
- src/caffe/parallel.cpp:70:1:错误:“参数”未命名类型
- OpenCV 错误:ERClassifierNM1 中的错误参数
- 为什么显示错误:参数太少而无法正常工作
- 算法错误:参数无法从对中掩盖 1
- 类模板方法的专用化,类型名称是类模板 - 错误:参数处的类型/值不匹配
- C++连接函数错误-参数无效
- 错误:参数无效
- (自适应)opencv中的阈值错误(cvarrToMat中的错误参数(未知数组类型))
- OpenCV错误:cvGetSize中的错误参数(数组应该是CvMat或IplImage)
- OpenCV 2.2:未知函数中的错误参数,文件.. .ocv OpenCV modulescoresr
- 错误:参数声明的存储类说明符无效