Skip to content

Commit 7e6a721

Browse files
authored
Update ThreadWithClassMemberFunction.cpp
1 parent ed0cf63 commit 7e6a721

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

Linux/ThreadWithClassMemberFunction.cpp

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2+
/*
3+
Below program shows how to pass the class member function to thread for thread creation .
4+
*/
15
#include<stdio.h>
26
#include<iostream>
37
#include<pthread.h>
@@ -13,11 +17,7 @@ class ThreadClass {
1317
};
1418

1519
void* ThreadClass::execute() {
16-
pthread_attr_t attr;
17-
size_t stackSize = 0;
18-
pthread_attr_getstacksize(&attr,&stackSize);
19-
cout<<" \ninsidse the execute without param - thread member function and stack size =>" <<pthread_self()<<" Stack size " << stackSize<<endl<<"\n";
20-
20+
cout<<" \ninsidse the execute without param - thread member function " <<pthread_self()<<endl<<"\n";
2121
return nullptr;
2222
}
2323

@@ -38,15 +38,11 @@ int main() {
3838
ThreadClass* threadPtr = new ThreadClass();
3939

4040
//THREADFUNC callbackFunc = (THREADFUNC)&ThreadClass::execute;
41-
//callbackFunc(threadPtr);
4241

43-
int id1 = pthread_create(&threadId1,nullptr,(THREADFUNC)&ThreadClass::execute,nullptr);
42+
int id1 = pthread_create(&threadId1,nullptr,(THREADFUNC)&ThreadClass::execute,threadPtr); // passing the this pointer to execute() i.e threadPtr
4443
int id2 = pthread_create(&threadId2,nullptr,ThreadClass::threadFunc,nullptr);
4544

4645
cout<<" \ninsidse the main - function =>" <<pthread_self()<<endl<<"\n";
47-
//printf("\n Address of the function %p\n",callbackFunc);
48-
49-
printf("\n Address of the function using directly %p\n",&ThreadClass::execute);
5046
pthread_exit(nullptr);
5147
return 0;
52-
}
48+
}

0 commit comments

Comments
 (0)