how to use member function for pthread_create

Hi,

pthread API is C API, we should pass global function as 3rd argument for pthread_create and which sould be of type void * and fourth argument should of void *.

When we use member function for pthread_create, member function is of class type, global to class itself not outside class and when we invoke the function this pointer comes into picture.
I know this can achived with static memebr function, but is there any waywe could achive this using memebr function?

pthread_create(&id,pthread_attr,golbal-function,void *);

Thanks
Create a static member function that takes the object pointer as argument and calls the appropriate non-static member function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <pthread.h>
#include <iostream>

struct A
{
	void memberFunction()
	{
		std::cout << "hello!" << std::endl;
	}
	static void* staticFunction(void* p)
	{
		static_cast<A*>(p)->memberFunction();
		return NULL;
	}
};

int main()
{
	A a;
	pthread_t id;
	pthread_create(&id, NULL, A::staticFunction, &a);
	pthread_join(id, NULL);
}
Hi Peter,

Thanks for the mail. yes you are right. But other than using the static memeber can't we use normal member function as an argument to pthread_create?

No. Pthreads is a C API and C doesn't have member functions.
Topic archived. No new replies allowed.