Click to See Complete Forum and Search --> : Execption on Threads - HELP ME


kadarb
January 2nd, 2003, 11:48 AM
I have an application which runs the following routines.
1. RunThreads
Is activated whenever start button is pressed.
It activates 3 threads. One of them is "TCPIPThreadFunc".

Once in a while I have protection error in the second
line of "TCPIPThreadFunc" thread because "pTheApp->m_pMainWnd"
is garbaged.
Is it possible that the thread is activated before
pTheApp->m_pThreadTCPIP gets it's value ??? If not what is the
problem?!?!?!

Thanks

BOOL RunThreads()
{
//run TCPIP thread /*********** T C P I P *****************/
CDynTCPIPApp * pTheApp = ( CDynTCPIPApp * )AfxGetApp();

pTheApp->m_pThreadTCPIP = AfxBeginThread( TCPIPThreadFunc, 0 );
if( !pTheApp->m_pThreadTCPIP )
{
MessageBox( NULL, "TCP IP Thread can't be created",
"Thread Info", MB_OK|MB_ICONSTOP );
return FAILURE;
}
.
.
.
.
.
.
}


UINT TCPIPThreadFunc( LPVOID pParam )
{
CDynTCPIPApp * pTheApp = ( CDynTCPIPApp * )AfxGetApp();

pTheApp->m_pThreadTCPIP->m_pMainWnd = pTheApp->m_pMainWnd;
.
.
.
.
.
}

TheCPUWizard
January 2nd, 2003, 11:55 AM
You have isolated the race condition. The value is not assigned till after the AfxBeginThread returns.

One solution is to start the thread suspended (you can then get the id information. and start the thread.

The alternative is to improve your design to avoid this type of "circular" references.

hope this helps.

kadarb
January 2nd, 2003, 12:06 PM
I'm trying for days to debug an application which I received and there are some bugs that I can't catch
Thanks a lot that is was I suspected!

:p