Question on Thread control functions and availability of member variables.
Posted on 2004-10-07
If I launch a thread from within an object:
m_pThread = AfxBeginThread(ThreadProcessProc, this, THREAD_PRIORITY_NORMAL, 0, 0, NULL );
and define my ThreadProcessProc as follows:
UINT CVideoCapture::ThreadProcessProc( LPVOID a_pParam )
CVideoCapture* pInstance = (CVideoCapture*) a_pParam;
Here I've passed "this" as the param to the control function, and so within the ThreadProcessProc method, I cast a_pParam to (CVideoCapture*) so that the calling object is availalbe within the thread's controlling function.
This is correct so far right?
I'm still a bit confused though, if I want to access any member variables or functions defined in the calling object, do I have to do so via this pointer to the calling object?
For example, say the object that is launching the thread has a member variable m_pSomeMember, and an accessor for that called GetSomeMember(). Within my thread control function, if I want to access m_pSomeMember, do I have to do this:
if ( pInstance->GetSomeMember() == 1 )
or can I access it directly like this:
if (m_pSomeMember == 1)
Is this where static members come into play? If I want direct access to a member variable from within a thread controlling function, I have to declare it statically, ie.
CVideoCapture::CVideoCapture() // constructor
What if the member variable my thread control function needs access to is not static? Do I need to implement an accessor method for it which I'd call via the local pInstance reference I cast from the a_pParam?
I hope this makes sense! I'm still just a bit confused.