2010-03-17 78 views
2
pthread_t thread1; 
pthread_create(&thread1,NULL,.......,NULL); 
// Here I want to attach a thread to a member function of class 

如何在上面的代码中传递一个类的成员函数。在pthread中附加一个类的成员函数

+0

不要忘了捕获所有异常。如果一个线程退出,因为一个异常完全解开了线程堆栈,那么你的程序可能会终止。 – 2010-03-17 08:59:33

回答

4

你需要创建一个免费的extern "C"功能蹦床:

class foo 
{ 
public: 
    void *thread_func(); 
}; 

extern "C" void *thread_func(void *arg) 
{ 
    return static_cast<foo *>(arg)->thread_func(); 
} 

foo f; 
pthread_create(..., thread_func, &f); 
相关问题