Re: how to terminate thread in vc++ by Simon
Simon
Sat Nov 26 05:06:56 CST 2005
As Scott indicated, PostQuitMessage is only of use if you are running a
message loop in the worker thread, which we guess you are not.
The easiest way is to have a volatile variable (an integer called quitNow,
say) accessible by both the threads which is set to zero before the worker
thread starts. The worker thread polls this flag every so often and
terminates via the usual mechanism (returning) when it finds the flag to be
nonzero. The controlling thread sets the flag to nonzero when it wishes the
worker thread to terminate. There is no need to interlock access to this
member variable, since we are just looking for a /change/ in the value and
/any/ change will serve to indicate to the worker thread that it should
terminate, even if the setting of the variable is not atomic and it goes to
some random value in between. Since you can't know which poll the worker
thread will actually find the flag to be nonzero, this is no good if you
want the thread to exit deterministically after any particular event, but is
useful if you just want it to exit as soon as it reasonably can.
S.
"raghu" <raghunandan_1081@yahoo.com> wrote in message
news:1132928251.594334.92130@g43g2000cwa.googlegroups.com...
> hi Everybody, I am using postquitmessage(GetIndexThread(<thraedname>,
> NULL)) function to terminate the thread just below the calling
> function.
>
> UINT CHRS_MoleDlg::Thread_Camera(LPVOID param)
> {
> CHRS_MoleDlg *self=(CHRS_MoleDlg *)param;
> for(;;)
> self->OnCameraThread();
> PostQuitMessage(GetExitCodeThread(Thread_Camera , NULL));
> return 0;
> }
>
> but still i am facing problem , actually i dont know exactly where i
> should write postquitmessage() what function i should used to terminate
> thraed.
> please tell me.
>