Need Help in VC++ Threading
Hi,
I need help regarding VC++ Threading
Here is my piece of code:
Code:
CWinThread* thread=NULL;
thread= AfxBeginThread( LoadData(), this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED );
thread->m_bAutoDelete = false;
thread->ResumeThread();
SUL_WaitForEndOfThread( thread->m_hThread );
I am getting access violation error in "SUL_WaitForEndOfThread( thread->m_hThread );"
When my compiler reaches "thread->ResumeThread();" it doesn't call LoadData().
Instead it moves to the next statement SUL_WaitForEndOfThread( thread->m_hThread ); and throws me a access violation error.
Need help in this. :)
Reagrds
Re: Need Help in VC++ Threading
- What is SUL_WaitForEndOfThread?
- What is LoadData? Why and how is it supposed to be called?
- What exactly gets this "access violation error": the compiler or your application?
Re: Need Help in VC++ Threading
My SUL_WaitForEndOfThread() contains the following:
Code:
void SUL_WaitForEndOfThread( HANDLE hThread )
{
DWORD appThreadID = AfxGetApp()->m_nThreadID;
DWORD threadID = AfxGetThread()->m_nThreadID;
DWORD result;
int count = 20;
if ( appThreadID == threadID )
{
SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from main thread"), hThread );
MSG* pMsg = &AfxGetThread()->m_msgCur;
count = 100;
while ( TRUE )
{
result = ::WaitForSingleObject( hThread, 0 );
if ( count == 100 )
{
switch ( result )
{
case WAIT_ABANDONED:
SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from main thread - WAIT_ABANDONED - %d"), hThread, result );
break;
case WAIT_OBJECT_0:
SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from main thread - WAIT_OBJECT_0 - %d"), hThread, result );
break;
case WAIT_FAILED:
SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from main thread - WAIT_FAILED - %d"), hThread, result );
break;
case WAIT_IO_COMPLETION:
SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from main thread - WAIT_IO_COMPLETION - %d"), hThread, result );
break;
case WAIT_TIMEOUT:
SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from main thread - WAIT_TIMEOUT - %d"), hThread, result );
break;
default:
SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from main thread - %d"), hThread, result );
break;
}
count = 0;
}
count++;
if ( result != WAIT_TIMEOUT )
{
break;
}
if ( ::PeekMessage( pMsg, NULL, NULL, NULL, PM_REMOVE ) )
{
if ( !AfxGetThread()->PreTranslateMessage( pMsg ) )
{
::TranslateMessage( pMsg );
::DispatchMessage( pMsg );
}
}
else
{
Sleep( 50 );
}
}
}
else
{
SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from background thread"), hThread );
while ( ( result = ::WaitForSingleObject( hThread, 50 ) ) == WAIT_TIMEOUT )
{
if ( count == 20 )
{
SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from background thread - WAIT_TIMEOUT - %d"), hThread, result );
count = 0;
}
count++;
Sleep( 100 );
}
switch ( result )
{
case WAIT_ABANDONED:
SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from background thread - WAIT_ABANDONED - %d"), hThread );
break;
case WAIT_OBJECT_0:
SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from background thread - WAIT_OBJECT_0 - %d"), hThread );
break;
case WAIT_FAILED:
SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from background thread - WAIT_FAILED - %d"), hThread );
break;
case WAIT_IO_COMPLETION:
SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from background thread - WAIT_IO_COMPLETION - %d"), hThread, result );
break;
case WAIT_TIMEOUT:
SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from background thread - WAIT_TIMEOUT - %d"), hThread, result );
break;
default:
SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from background thread - %d"), hThread, result );
break;
}
//::WaitForSingleObject( hThread, INFINITE );
}
SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - end"), hThread );
}
LoadData() is the thread function to be called once a thread is created. It contains the block of code to be executed for some operations.
The compiler gives me this Access Violation Error.
Re: Need Help in VC++ Threading
Quote:
Originally Posted by
p_zshk
...
The compiler gives me this Access Violation Error.
In what line of your code?
Re: Need Help in VC++ Threading
"while ( ( result = ::WaitForSingleObject( hThread, 50 ) ) == WAIT_TIMEOUT )"
this line gives me the Acess Violation.
Re: Need Help in VC++ Threading
This your call of AfxBeginThread is wrong:
Quote:
Originally Posted by
p_zshk
Hi,
Here is my piece of code:
Code:
thread= AfxBeginThread( LoadData(), this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED );
The correct one is
Code:
thread= AfxBeginThread( LoadData, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED );
Re: Need Help in VC++ Threading
oops...done a typo error here...sorry for that...
i have written the syntax the proper way...
thread= AfxBeginThread( LoadData, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED );
may be there is some other problem...
Re: Need Help in VC++ Threading
Show us your LoadData signature
Re: Need Help in VC++ Threading
Here is the declaration:
Code:
static UINT LoadData( LPVOID aParam );
Re: Need Help in VC++ Threading
Actually I can't see any reason to create the thread suspended as long as you resume it immediately. So try to get rid of the stuff and see if your problem went off.
Re: Need Help in VC++ Threading
I am setting m_bAutoDelete to false, that's why I am creating a thread in suspended mode and then starting the thread through ResumeThread().
Re: Need Help in VC++ Threading
Well, if you don't start a thread in suspended mode (and don't call ResumeThread) does the problem persist?
If Yes - create a small project reproducing this behaviour and post it (in zip archive) to the Forum.
Re: Need Help in VC++ Threading
Quote:
I am setting m_bAutoDelete to false, that's why I am creating a thread in suspended mode and then starting the thread through ResumeThread().
You never need to suspend thread for this.
Quote:
m_bAutoDelete
Specifies whether to destroy the object at thread termination.
It has nothing to do with the thread, it's just about CWinThread object, and nothing more than this.