Re: Thread creation
From: Roy Fine (rlfine_at_twt.obfuscate.net)
Date: 04/08/04
- Previous message: Jase: "Re: Status bar - INS??"
- In reply to: Moiz Mehmood: "Thread creation"
- Next in thread: Moiz Mehmood: "Re: Thread creation"
- Reply: Moiz Mehmood: "Re: Thread creation"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 7 Apr 2004 21:55:52 -0400
Moiz
Your code looks good at first glance - in fact it is quite similar from the
model that I use - except I would turn off the autodelete and forego the
duplicate handle.
I tend to wrap the whole mess in a separate class and keep all things
related encapsulated therein. I have adopted/adapted my std model to the
methods you presented, and all seems to work well.. I cobbled together a
quick hack and it is appended here for your review.
My guess now would be that the problem lies elsewhere - specifically, what
are you doing in the worker thread code with the "this" pointer - thread
safe stuff I would assume.
On which line of code do you get the "crash", and exactly what is the
crash - i.e. memory access exception, unhandled exception, etc... What does
the call stack look like after the "crash"? Can you trace back to somewhere
in your code?
regards
roy fine
/* ***************************** */
class MYTHREAD{
private:
MYTHREAD();
int ival;
CString strVal;
public:
CWinThread *pthread;
HANDLE thrdhandle;
public:
~MYTHREAD(){
::WaitForSingleObject(thrdhandle,INFINITE);
::CloseHandle(thrdhandle);
}
MYTHREAD(int p_ival,CString p_strVal){
ival = p_ival;
strVal = p_strVal;
pthread = AfxBeginThread(MYTHREAD::Worker, this,NULL,
THREAD_PRIORITY_NORMAL, CREATE_SUSPENDED,NULL);
::DuplicateHandle(GetCurrentProcess(),pthread->m_hThread,GetCurrentProcess()
,
&thrdhandle,0,FALSE,DUPLICATE_SAME_ACCESS);
pthread->ResumeThread();
}
static unsigned int Worker(void *p){
int someVal;
CString someStr;
MYTHREAD *baseClass = (MYTHREAD *)p;
someVal = baseClass->ival*baseClass->ival;
CString tstr;
tstr.Format("%s. and the square of %d is
%d",baseClass->strVal,baseClass->ival,someVal);
baseClass->strVal = tstr;
::Sleep(10000);
return 0;
}
};
"Moiz Mehmood" <anonymous@discussions.microsoft.com> wrote in message
news:04F995BC-F219-4CAA-9894-C35516D05E09@microsoft.com...
>
> Hello,
> I am trying to create a thread using AfxBeginThread. I then wait for that
thread to finish using WaitForsingleObject. My code is crashing
consistently. Below is the code snippet which is causing the crash. Can
anyone point out what I am doing wrong?
>
> This code is part of a message handler.
>
> Thanks is advance
> Moiz
>
> CWinThread* pThread = AfxBeginThread(LocalThread, this,
THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
> HANDLE threadHandle;
> ::DuplicateHandle(GetCurrentProcess(),
> pThread->m_hThread,
> GetCurrentProcess(),
> &threadHandle,
> 0,
> FALSE,
> DUPLICATE_SAME_ACCESS);
> pThread->ResumeThread();
> ::WaitForSingleObject(threadHandle, INFINITE);
> ::CloseHandle(threadHandle);
>
- Previous message: Jase: "Re: Status bar - INS??"
- In reply to: Moiz Mehmood: "Thread creation"
- Next in thread: Moiz Mehmood: "Re: Thread creation"
- Reply: Moiz Mehmood: "Re: Thread creation"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|