类中不同成员函数的cpp中的pthread
pthread in cpp for different member functions in a class
我从一个简单的例子开始http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm我需要将不同的cpp和标头划分为以下
Process_Images.h 中的类定义
void PrintHello(void* threadid);
在Process_Images.cpp 中
void ProcessImages::PrintHello(void* threadid)
{
long tid;
tid = (long)threadid;
std::cout << "Hello World! Thread ID, " << tid << std::endl;
pthread_exit(NULL);
}
在主要功能中
ProcessImages PI;
pthread_t threads[2];
pthread_create(&threads[0],NULL,PI.PrintHello,(void *)i);
错误为-->
/home/nvidia/Desktop/cms/tools/vibrante-vcm30t124-linux/cmsapplication_export/cmsapplication/sampleThread.cpp:333:69: error: cannot convert ���ProcessImages::PrintHello��� from type ���void (ProcessImages::)(void*)��� to type ���void* (*)(void*)���
pthread_create(&threads[0],NULL,CarDetLEFT.PrintHello,(void *)i);
^
有什么建议吗?
因为我在问题中看到了C++11标签,所以绝对不需要经过pthread路由!
std::thread thr(&ProcessImages::PrinteHello, &PI, &i);
会对你有好处的!
两个问题:
- 线程函数必须返回
void*
,而不是void
- 线程函数不能是(非静态)成员函数
更多详细信息可在pthread_create
的参考页面上找到。
因此,将ProcessImages::PrintHello
的返回类型更改为void*
,并使其成为static
:
class ProcessImages {
//<SNIP>
public :
static void* PrintHello(void* threadid);
//<SNIP>
};
那么这应该会更好:
pthread_create(&threads[0], NULL, &ProcessImages::PrintHello, (void*) i);
相关文章:
- 内置函数可查看CPP中的成员变量
- 无法按cpp中的值返回矢量
- 使用cpp中的结构和函数的多个学生条目
- 在 cpp 中的平面缓冲区中序列化对象
- CPP 中的瓦尔格林德和记忆泄漏:"Conditional jump or move depends on uninitialised values"
- cpp 中的特征矩阵
- 如何循环访问 cpp 中的函数返回的字符指针数组
- 像 CPP 中的控制台一样的应用程序
- 将一系列数字映射到 CPP 中的值的简单方法
- 和 (&&) CPP 中的指针
- 静态 CPP 中的非静态成员
- CPP 中的字符串加法关联性
- 如何从 cpp 中的函数返回自定义类对象?
- 如何从主.cpp中的cpp文件中获取功能结果?
- 为什么'Hello'与CPP中的'World'进行比较时被打印出来?
- 有没有办法从 cpp 中的文本生成条形码图像?
- 为什么 const YAML::Node 对象的行为不像 yaml-cpp 中的类似值的对象?
- 使用Regex解析cpp中的字符串并创建映射
- afxmem.cpp中的对象初始化差异
- 根据 cpp 中的某些条件切换流