我想设计一个曾经创建的排序的主动对象,本质上运行在它自己的线程。到目前为止,我所做的是创建一个包含pthread实例变量的类,然后在该类的构造函数中,它应该发送pthread。Pthread实例变量从类
由于pthread_create()接受一个函数参数,我将它传递给我的类实现文件中的run()函数。
截至目前,我的run()函数不是类的一部分,它只是坐在实现文件,但是当我试图编译它,我得到一个错误说:
"error: ‘run’ was not declared in this scope"
现在我明白了为什么函数run()超出范围,但将run()作为私有函数添加到我的活动对象类是否正确,或者如果存在多个这样的对象会导致其他问题?哎呀,它会导致只有其中一个实例化的问题?
好的,这里是代码,我只是不认为它很重要。这里是MyClass.hpp
class MyClass {
private:
pthread_t thread;
ObjectManager *queue;
int error;
// just added this, but the compiler still doesn't like it
void *run(void *arg);
public:
MyClass();
~MyClass();
void start();
}
这里是实施,MyClass.cpp:
#include "MyClass.hpp"
void MyClass::start() {
if (queue == NULL)
return;
int status = pthread_create(&thread, NULL, run, (void *) queue);
if (status != 0) {
error = status;
return;
}
}
void *MyClass::run(void *arg) {
bool finished = false;
while (!finished) {
// blah blah blah
}
return NULL;
}
请显示代码,否则将很难提供任何帮助 – nos
好吧,只是尝试将它添加为类函数,并且由于类型不匹配而无法编译。它正在寻找void *(*)(void *)并且获取void *(MyClass ::)(void *)。 –
[pthread Function from a Class]的可能重复(http://stackoverflow.com/questions/1151582/pthread-function-from-a-class) –