0

I am passing a class member function to pthread_create and getting below error. I know there are so many queries already on stackoverflow and they are saying that make a static helper around class member function and pass that static function in the pthread and function callback as last argument in the pthread_create but in my case, the member functions has arguments also. So, my problem is slightly different one. Where should I pass the member function arguments ? Any help would be highly appreciated.

#include <stdio.h>
#include <pthread.h>

struct Point
{
  int y;
  int z;
};

class A
{
  int x;

  public:
    int getX();
    void setX(int val) {x = val;}

    void* func(void* args) 
    {
      Point p = *(Point *)args;

      int y = p.y;
      int z = p.z;

      return (void *)(x + y + z); 
    }


    void myFunc()
    {
      int y = 12;
      int z = 2;

      pthread_t tid;

      Point p;
      p.y = y;
      p.z = z;

      pthread_create(&tid, NULL, func, &p);
      pthread_join(tid, NULL);
    }
};

int main(int argc, char *argv[])
{

  A a;

  a.myFunc();

  return 0;
}

Error :

 classThreads.c: In member function ‘void A::myFunc()’:
classThreads.c:40:42: error: cannot convert ‘A::func’ from type ‘void* (A::)(void*)’ to type ‘void* (*)(void*)’
       pthread_create(&tid, NULL, func, &p);
Bhawan
  • 2,441
  • 3
  • 22
  • 47
  • The problem is not with the arguments but the `this` pointer for the member function. – Justin Finnerty Dec 11 '17 at 10:39
  • I know @JustinFinnerty and I also know the solution but the solution is for those member functions who does not have any arguments, i want a solution for those member functions, who has arguments. – Bhawan Dec 11 '17 at 10:40
  • 2
    Possible duplicate of [cannot convert '\*void(MyClass::\*)(void\*) to void\*(\*)(void\*) in pthread\_create function](https://stackoverflow.com/questions/12006097/cannot-convert-voidmyclassvoid-to-voidvoid-in-pthread-create-fu) – user7860670 Dec 11 '17 at 10:41
  • Not duplicate as question specifically asks how to do this with member function that takes arguments. – Justin Finnerty Dec 11 '17 at 10:48
  • 1
    Why not use `std::thread`? – Maxim Egorushkin Dec 11 '17 at 11:22

1 Answers1

0

The problem is you are not passing a this pointer to the thread create method. This needs to be added to the argument list.

#include <utility>
#include <pthread.h>

struct Point
{
  int y;
  int z;
};

class A
{
  int x;

  public:
    int getX();
    void setX(int val) {x = val;}

    static void* func(void* args) 
    {
      std::pair< A*, Point > tp = *(std::pair< A*, Point > *)args;

      int y = tp.second.y;
      int z = tp.second.z;

      return (void *)(tp.first->x + y + z); 
    }


    void myFunc()
    {
      int y = 12;
      int z = 2;

      pthread_t tid;

      Point p;
      p.y = y;
      p.z = z;
      std::pair< A*, Point > tp( this, p );
      pthread_create(&tid, NULL, func, &tp);
      pthread_join(tid, NULL);
    }
};

int main(int argc, char *argv[])
{

  A a;

  a.myFunc();

  return 0;
}

You can use the std::tuple class to pass the this pointer and more than one argument.

Justin Finnerty
  • 329
  • 1
  • 7