Click here to Skip to main content
15,913,323 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralTermination of a thread Pin
Vassili16-Apr-03 2:11
Vassili16-Apr-03 2:11 
GeneralRe: Termination of a thread Pin
David Crow16-Apr-03 2:16
David Crow16-Apr-03 2:16 
GeneralRe: Termination of a thread Pin
Vassili16-Apr-03 3:27
Vassili16-Apr-03 3:27 
GeneralRe: Termination of a thread Pin
David Crow16-Apr-03 3:33
David Crow16-Apr-03 3:33 
GeneralRe: Termination of a thread Pin
Vassili16-Apr-03 4:24
Vassili16-Apr-03 4:24 
GeneralRe: Termination of a thread Pin
David Crow16-Apr-03 4:31
David Crow16-Apr-03 4:31 
GeneralRe: Termination of a thread Pin
Rage16-Apr-03 4:52
professionalRage16-Apr-03 4:52 
GeneralRe: Termination of a thread Pin
Mike Upton16-Apr-03 2:26
Mike Upton16-Apr-03 2:26 
The sensible way to terminate a thread is to make it terminate itself - generally by setting some sort of flag to signal that the thread should terminate. In its simplest form, the code looks something like this:

//assumes your bTerminateFlag variable is defined elsewhere - you'd
//probably actually do this with some sort of class, and pass a pointer
//to the class instance to your thread function, but I'm keeping this simple
extern bool bTerminateFlag;
 
int YourThreadFunction(LPVOID pParam)
{
    while (!bTerminateFlag)
    {
        do_something...
    }
    return exit_code;
}


And your function to terminate the thread would look something like this:
int TerminateWorkerThread(HANDLE hWorker)
{
    bTerminateFlag = true;
    if (WaitForSingleObject(hWorker, INFINITE)!=WAIT_OBJECT_0)
    {
        //an error has occurred
        //handle it somehow
    }
    else
    {
        return GetExitCodeThread(hWorker);
    }
}

This is all very simplified - you'd want proper error checking, you probably shouldn't use a global variable as the terminate flag, and you shouldn't really use WaitForSingleObject in a thread with a message loop. But the principle is there. There's generally very little reason to use TerminateThread, and it can lead to all sorts of problems when your thread is doing something worthwhile, and holding resources open, because the terminated thread doesn't get to do any cleanup.



"We are the knights who say Ni" (The Knights Who Say Ni)
QuestionHow to describe such objects using C++? Pin
George216-Apr-03 1:30
George216-Apr-03 1:30 
AnswerRe: How to describe such objects using C++? Pin
perlmunger16-Apr-03 7:10
perlmunger16-Apr-03 7:10 
GeneralXOR mode Pin
pankajdaga16-Apr-03 1:21
pankajdaga16-Apr-03 1:21 
GeneralRe: XOR mode Pin
Dominik Reichl16-Apr-03 1:58
Dominik Reichl16-Apr-03 1:58 
GeneralUnicode windows Pin
Hugo Hallman16-Apr-03 1:10
Hugo Hallman16-Apr-03 1:10 
GeneralRe: Unicode windows Pin
Anders Molin16-Apr-03 4:17
professionalAnders Molin16-Apr-03 4:17 
GeneralBest way to completely empty structure.. Pin
IrishSonic16-Apr-03 1:02
IrishSonic16-Apr-03 1:02 
GeneralRe: Best way to completely empty structure.. Pin
Dominik Reichl16-Apr-03 2:03
Dominik Reichl16-Apr-03 2:03 
GeneralRe: Best way to completely empty structure.. Pin
JT Anderson16-Apr-03 6:35
JT Anderson16-Apr-03 6:35 
GeneralRe: Best way to completely empty structure.. Pin
Gary R. Wheeler18-Apr-03 5:14
Gary R. Wheeler18-Apr-03 5:14 
GeneralWhich way is better to overload operator >> Pin
George215-Apr-03 23:50
George215-Apr-03 23:50 
GeneralRe: Which way is better to overload operator >> Pin
Joaquín M López Muñoz16-Apr-03 3:34
Joaquín M López Muñoz16-Apr-03 3:34 
GeneralRe: Which way is better to overload operator >> Pin
George216-Apr-03 3:43
George216-Apr-03 3:43 
Generallocale currency Pin
vcseeker15-Apr-03 21:57
vcseeker15-Apr-03 21:57 
GeneralRe: locale currency Pin
David Crow16-Apr-03 2:18
David Crow16-Apr-03 2:18 
GeneralIE toolbar disappearing buttons Pin
xenonii15-Apr-03 20:54
xenonii15-Apr-03 20:54 
GeneralRe: IE toolbar disappearing buttons Pin
xenonii16-Apr-03 2:22
xenonii16-Apr-03 2:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.