For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > how to terminate thread in vc++









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to terminate thread in vc++
raghu

2005-11-25, 7:02 pm

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.

Scott McPhillips [MVP]

2005-11-25, 7:02 pm

raghu wrote:
> 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(Threa
d_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.


Does your thread contain a message loop? It does not appear to from
your code summary. PostQuitMessage is only used to notify your message
loop that you wish to exit. If you are not using a message loop then do
not call PostQuitMessage. Just return and your thread will exit.

--
Scott McPhillips [VC++ MVP]

Simon Trew

2005-11-26, 7:57 am

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.
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com