There is no function that executes when a thread exits in Windows (at least not using the standard Windows threading API).

There is the option of using a WaitFor/Single,Multiple/Object() to wait for a thread to finish, and I suppose this will actually work for checking if ANY of the threads have been killed/terminated. It probably requires a separate "watcher" thread to handle the watching, and dealing with the consequences of a particular thread dieing.

--
Mats

Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.

The best way, I would say is to do it the MFC way.
Make a CThread class that encapsulates a thread. Then make a virtual function for InitThread and ExitThread that are called when a thread begins and ends respectively. The best way is also to overload start/resume/sleep/kill too, so that if even if you kill the thread, it will call ExtThread first and then kill it (or use a flag).

Originally Posted by Adak

io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.

Originally Posted by Salem

You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

The best way, I would say is to do it the MFC way.
Make a CThread class that encapsulates a thread. Then make a virtual function for InitThread and ExitThread that are called when a thread begins and ends respectively. The best way is also to overload start/resume/sleep/kill too, so that if even if you kill the thread, it will call ExtThread first and then kill it (or use a flag).

But you have to be careful if you use "ExitThread" from a another thread, if you are also use TLS - since TLS uses the "current thread" [1] for it's data.

[1] Ok, so it's not calling the OS to ask what the current thread is, but rather the OS when it switches from one thread to another, loads FS (I think) with the current threads local storage, so you can offset off that to get your TLS data.

--
Mats

Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.

Terminating a thread prematurely is almost as simple: Call AfxEndThread from within the thread. Pass the desired exit code as the only parameter. This stops execution of the thread, deallocates the thread's stack, detaches all DLLs attached to the thread, and deletes the thread object from memory.

AfxEndThread must be called from within the thread to be terminated. If you want to terminate a thread from another thread, you must set up a communication method between the two threads.

--
Mats

Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.