2017-03-16 87 views
0

如何将成员函数作为参数传递给pthread_create函数?如何将对象的成员函数作为参数传递给pthread_create?

实施例:

class A 
    { 
     public: 
     A(){} 

     void funcForThread(Person p) { 
      //code... 
     } 

     void createThread(Person p) { 
      pthread_t thread1; 
      pthread_attr_init (&attr); 
      pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE); 
      pthread_create (&thread1, &attr, /* what to write here*/); // need to pass funcForThread and Person p object there 
     } 

     private: 
     pthread_attr_t attr; 
    }; 
+0

这不是C,因为C没有类或成员函数。 – Barmar

+1

您需要传递代理功能。 – Barmar

+0

@Barmar我不知道代理功能是什么,请你提供一个例子吗? – stilltryingbutstillsofar

回答

0

就这样

void * 
call_member(void *data) 
{ 
    A *a = reinterpret_cast<A *>(data); 
    a->member(); 

    return NULL; 
} 

然后,

pthread_create(&thread1, &attr, call_member, this); 
相关问题