如何将带有object的成员函数作为参数传递给pthread_create?

时间:2017-03-16 21:56:13

标签: c++ pthreads

如何将带有object的成员函数作为参数传递给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;
    };

1 个答案:

答案 0 :(得分:0)

就像这样

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

    return NULL;
}

然后,

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