pthread_t thread1;
pthread_create(&thread1,NULL,.......,NULL);
// Here I want to attach a thread to a member function of class
如何在上面的代码中传递类的成员函数。
答案 0 :(得分: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);