R
Rookie
Hi,
I was writing a simple multi-threaded program (code below). My intent is to
invoke a member function of a class that creates two threads - each thread
executes another member function of the same class. The pthread_create
function used to create a thread requires a pointer to the function that
will be executed. I tried all the possible syntax ideas that came to mind,
but I am not able to pass it a pointer to the member function of the class.
Can someone please point out my error. Thanks.
class test
{
public:
void* funcA(void* dud)
{
int i=0;
char c,*d;
d=(char*)dud;
c=*d;
for(i=0;i<100;i++)
printf("%c",c);
}
int funcB()
{
pthread_t threadId1,threadId2;
char c,d;
c='a';
pthread_create(&threadId1,NULL,this->funcA,&c); //I keep getting
compilation errors for this line. I tried a::funcA, &a::funcA, funcA,
&funcA. None of them work.
d='b';
pthread_create(&threadId2,NULL,this->funcA,&d); //I keep getting
compilation errors for this line
pthread_join(threadId2,NULL);
pthread_join(threadId1,NULL);
perror("say what");
}
};
main()
{
test obj;
obj.funcB();
}
I was writing a simple multi-threaded program (code below). My intent is to
invoke a member function of a class that creates two threads - each thread
executes another member function of the same class. The pthread_create
function used to create a thread requires a pointer to the function that
will be executed. I tried all the possible syntax ideas that came to mind,
but I am not able to pass it a pointer to the member function of the class.
Can someone please point out my error. Thanks.
class test
{
public:
void* funcA(void* dud)
{
int i=0;
char c,*d;
d=(char*)dud;
c=*d;
for(i=0;i<100;i++)
printf("%c",c);
}
int funcB()
{
pthread_t threadId1,threadId2;
char c,d;
c='a';
pthread_create(&threadId1,NULL,this->funcA,&c); //I keep getting
compilation errors for this line. I tried a::funcA, &a::funcA, funcA,
&funcA. None of them work.
d='b';
pthread_create(&threadId2,NULL,this->funcA,&d); //I keep getting
compilation errors for this line
pthread_join(threadId2,NULL);
pthread_join(threadId1,NULL);
perror("say what");
}
};
main()
{
test obj;
obj.funcB();
}