C++ 在pthread中附加类的成员函数
如何在上述代码中传递类的成员函数。您需要创建一个免费的C++ 在pthread中附加类的成员函数,c++,c,C++,C,如何在上述代码中传递类的成员函数。您需要创建一个免费的extern“C”函数作为蹦床: pthread_t thread1; pthread_create(&thread1,NULL,.......,NULL); // Here I want to attach a thread to a member function of class class-foo { 公众: void*thread_func(); }; 外部“C”void*线程函数(void*arg) { 返回static
extern“C”
函数作为蹦床:
pthread_t thread1;
pthread_create(&thread1,NULL,.......,NULL);
// Here I want to attach a thread to a member function of class
class-foo
{
公众:
void*thread_func();
};
外部“C”void*线程函数(void*arg)
{
返回static_cast(arg)->thread_func();
}
福福;
pthread_create(…,thread_func,&f);
不要忘记捕获所有异常。如果由于异常而退出线程,则程序可能会终止。
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);