2010-03-17 87 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); 
相關問題