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);