-
September 9th, 2011, 01:38 AM
#1
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
-
September 9th, 2011, 01:59 AM
#2
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?
Victor Nijegorodov
-
September 9th, 2011, 02:09 AM
#3
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.
-
September 9th, 2011, 02:16 AM
#4
Re: Need Help in VC++ Threading
Originally Posted by p_zshk
...
The compiler gives me this Access Violation Error.
In what line of your code?
Victor Nijegorodov
-
September 9th, 2011, 02:25 AM
#5
Re: Need Help in VC++ Threading
"while ( ( result = ::WaitForSingleObject( hThread, 50 ) ) == WAIT_TIMEOUT )"
this line gives me the Acess Violation.
-
September 9th, 2011, 02:58 AM
#6
Re: Need Help in VC++ Threading
This your call of AfxBeginThread is wrong:
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 );
Victor Nijegorodov
-
September 9th, 2011, 03:50 AM
#7
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...
-
September 9th, 2011, 03:52 AM
#8
Re: Need Help in VC++ Threading
Show us your LoadData signature
Victor Nijegorodov
-
September 9th, 2011, 03:57 AM
#9
Re: Need Help in VC++ Threading
Here is the declaration:
Code:
static UINT LoadData( LPVOID aParam );
Last edited by p_zshk; September 9th, 2011 at 04:02 AM.
-
September 9th, 2011, 04:32 AM
#10
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.
Best regards,
Igor
-
September 9th, 2011, 06:27 AM
#11
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().
-
September 9th, 2011, 06:34 AM
#12
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.
Victor Nijegorodov
-
September 9th, 2011, 09:50 AM
#13
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().
You never need to suspend thread for this.
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.
Last edited by Igor Vartanov; September 9th, 2011 at 09:53 AM.
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|