从线程函数调用成员函数时为什么发生Segmentaion错误?

时间:2015-06-16 22:10:48

标签: c++ multithreading

我用c ++创建了一个代码来实现库管理。 我有一个调用函数的线程函数。

---另一个班级有实施---

class employee
{ 

private:

----datas--

pthread_t my_thread;

public:

static void* worker_thread(void *);
char * getName();//function already written to return name
char * getTime();//function already written to return Time
pthread_t getthread();//function already written to return thread variable
}



 void* Employee :: worker_thread(void *arg)
 {
   pthread_detach(pthread_self());
   cout<<"Employee Time :"<<((Employee *)arg)->getTime()<<endl; //got error segmentaion fault
    cout<<"Employee Name :"<<((Employee *)arg)->getName()<<endl;
 }

int implementation:: Book()
{
 int ret = 0; 
 Employee *emp = new Employee;
 Employee *temp = NULL;
 temp = emp->gethead();

   th = emp->getthread(); 
   ret =  pthread_create(&th,NULL,&Employee::worker_thread,&temp);
   ....
   ...
}

当我使用这些getTime()和getName()方法使用这些对象打印(或cout)值时,我得到了segmentaion错误..为什么?我正在使用链接在一起的对象列表..我的代码有问题吗?

1 个答案:

答案 0 :(得分:0)

我可以看到,您的void* Employee :: worker_thread(void *arg)定义缺少return声明。