Passing the member function pointer to the AfxBeginThread and its problems.
-
Hi I am writing this to help others who will also have the same problem Creating worker thread : CWinThread *pThread = AfxBeginThread(&MyThread,(LPVOID*)ptrParam); MyThread is the member function of CWorker class, and its prototype is UINT MyThread( LPVOID pParam ) ; Now while compiling I am getting this error Error 4 error C2665: ‘AfxBeginThread’ : none of the 2 overloads could convert all the argument types We know the function prototype is correct. MSDN ref : UINT __cdecl MyControllingFunction( LPVOID pParam ); should be the function prototype. We are also having the Exact prototype , so what could be the problem. Solution : All the Class member function will have one hidden parameter ( ie ) this pointer. So Actucal function prototype of MyThread is UINT MyThread(CWorker pthis , LPVOID pParam ) ; becoz of this, compiler gives this error. To Solve the problem 1) Creating one global wrapper funciton around MyThread() UINT wrapper(LPVOID pParam) { CWorker *p = new(std::nothrow) CWorker ; if(!p) return ; p->MyThread(pParam); return 0; } CWinThread *pThread = AfxBeginThread(&wrapper,(LPVOID*)ptrParam); wil work perfectly. 2) Declaring the member function as static static UINT MyThread( LPVOID pParam ) ; Note: static member funciton cant access non static member function and non static data member. Regards, WindowsPistha
-
Hi I am writing this to help others who will also have the same problem Creating worker thread : CWinThread *pThread = AfxBeginThread(&MyThread,(LPVOID*)ptrParam); MyThread is the member function of CWorker class, and its prototype is UINT MyThread( LPVOID pParam ) ; Now while compiling I am getting this error Error 4 error C2665: ‘AfxBeginThread’ : none of the 2 overloads could convert all the argument types We know the function prototype is correct. MSDN ref : UINT __cdecl MyControllingFunction( LPVOID pParam ); should be the function prototype. We are also having the Exact prototype , so what could be the problem. Solution : All the Class member function will have one hidden parameter ( ie ) this pointer. So Actucal function prototype of MyThread is UINT MyThread(CWorker pthis , LPVOID pParam ) ; becoz of this, compiler gives this error. To Solve the problem 1) Creating one global wrapper funciton around MyThread() UINT wrapper(LPVOID pParam) { CWorker *p = new(std::nothrow) CWorker ; if(!p) return ; p->MyThread(pParam); return 0; } CWinThread *pThread = AfxBeginThread(&wrapper,(LPVOID*)ptrParam); wil work perfectly. 2) Declaring the member function as static static UINT MyThread( LPVOID pParam ) ; Note: static member funciton cant access non static member function and non static data member. Regards, WindowsPistha
There's nothing new about this. It's been discussed on this forum dozens of times, and there are even articles about it.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
There's nothing new about this. It's been discussed on this forum dozens of times, and there are even articles about it.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
Yes you are right , I have red the below article before i posting. It did not talk about , why compiler gives error. so i thought of sharing here,I want to give something back to codeproject which gives lot of information to me. http://www.codeproject.com/KB/threads/memberthread.aspx[^] Regards, WindowsPistha
-
Yes you are right , I have red the below article before i posting. It did not talk about , why compiler gives error. so i thought of sharing here,I want to give something back to codeproject which gives lot of information to me. http://www.codeproject.com/KB/threads/memberthread.aspx[^] Regards, WindowsPistha
I searched and found three pages worth. Some of them include: http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=1510191[^] http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2166781[^] http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2071901[^] http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=1363953[^]
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
I searched and found three pages worth. Some of them include: http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=1510191[^] http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2166781[^] http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2071901[^] http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=1363953[^]
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
Thank you Mr. David here after i will double check before i posting. Regards, WindowsPistha