• Advertisement
Sign in to follow this  

[WinAPI] TerminateThread()

This topic is 4558 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

From MSDN:
Quote:
TerminateThread is used to cause a thread to exit. When this occurs, the target thread has no chance to execute any user-mode code and its initial stack is not deallocated.
But how to deallocate that initial stack. Is it needed?

Share this post


Link to post
Share on other sites
Advertisement
The TerminateThread function should be used as a last resort - usually threads should be allowed to return and stop of their own accord.

The TerminateThread will deallocate the memory used by your thread's stack but will not give you the chance to clean up any of the variables sitting on that stack. Thus, if your stack contains a pointer to some dynamically allocated memory (off the heap) the pointer will be lost (if it was the only copy) and none of your cleanup code will be called to make sure that the memory is properly deallocated.

arm.

Share this post


Link to post
Share on other sites
An example for my previous post


DWORD WINAPI thread(LPVOID)
{
// Allocate some memory
char* buffer = new char[1024];

// Do something for 500ms
Sleep(500);

// Cleanup and then exit thread
delete(buffer);
}

int main()
{
DWORD id;
HANDLE handle;

// Create running thread
handle = CreateThread(0,0,thread,0,0,&id)

// Kill thread before it executes cleanup code
Sleep(250);
TerminateThread(handle,0);
}

Share this post


Link to post
Share on other sites
Although it is not explicitly mentioned, I think it safe to assume that destructors for the current scope (and all enclosing ones) are not run, either. The thread is stopped dead in its tracks, regardless of what it is doing. Hence why the function is marked with so many dire warnings. :)

Share this post


Link to post
Share on other sites
I recently stumbled accross this problem and tried to solve it by continuously checking a state variable, which tells me whether to quit or not. But I don't know if there's are more convenient and elegant solution. Instead of checking the state variable I could have also processed the windows messages and therefore avoided the additional thread.
Is there a better way?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
arm's source understood.It's trivial.

But we have a running function in the thread.

aaa A2;
bbb A1;
Function(...);
ExitThread(...);


Function(...) stucks. I can terminate it w/ TerminateThread(). All new's are deleted. But any aaa or bbb are not deallocated beacause they are in stack. Hopw to deallocate them after the thread is terminated?

Share this post


Link to post
Share on other sites
As arm said, TerminateThread is a last resort. Good rule of thumb: don't use it.
Instead, occasionally check a variable to see if the thread should run. So:
void thread_function() {
while( m_shouldRun ) {
determineMeaningOfLife();
}
}

void endThread() {
m_shouldRun = false;
}


If your thread function blocks, you might need to wake it up in the endThread-method, using a signal or other means.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by DaBono
As arm said, TerminateThread is a last resort. Good rule of thumb: don't use it.
Instead, occasionally check a variable to see if the thread should run. So:
void thread_function() {
while( m_shouldRun ) {
determineMeaningOfLife();
}
}

void endThread() {
m_shouldRun = false;
}

If your thread function blocks, you might need to wake it up in the endThread-method, using a signal or other means.



while( m_shouldRun ) {
determineMeaningOfLife();
}
is a one function and I have no sources for it. Understood the idea?

Share this post


Link to post
Share on other sites
You're better off having a monitor process keep track of the main process doing this operation. If you detect the stuck thread then exit then entire process and have the monitor restart things.

Calling TerminateProcess, *especially* for code that you don't own or fully understand, is guaranteed to bite you in the ass eventually. In addition to the obvious memory and other resource leaks you can also very easily deadlock the entire process if the thread happens to own a vital critical section such as the ones in the DLL loader or heap manager.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement