Re: AfxBeginThread by Igor
Igor
Thu Jul 20 13:38:09 CDT 2006
mike7411@gmail.com wrote:
> Is there any automatic way to have AfxBeginThread or some other thread
> creation function call a class's member function directly?
>
> I don't see how the "this" pointer would get there.
AfxBeginThread and all other thread creation functions I know of take
LPVOID parameter and pass it along to thread proc. You can pass your
'this' pointer there, and have the thread proc turn around and call the
member function. Something like this:
template <class T, DWORD (T::*f)()>
DWORD WINAPI myThreadProc(LPVOID p) {
T* pThis = static_cast<T*>(p);
return (pThis->*f)();
}
class Thread {
public:
DWORD ThreadProc();
};
Thread t;
CreateThread(..., myThreadProc<Thread, &Thread::ThreadProc>, &t, ...);
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925