0

How do I pass member function with object as an argument to pthread_create function?

Example:

    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 Answers1

0

Just like this

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

    return NULL;
}

and then,

pthread_create(&thread1, &attr, call_member, this);
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97