0

I create new threads with AfxBeginThread. I have tried exiting the thread with AfxExitThread and just returning a value. Both of those are supposed to be valid ways of exiting the thread.

However when making calls to GetExitCodeThread I get STILL_ACTIVE even after several hours. Also when using (WaitForSingleObject(ahtRespondThreads[i], 0) == WAIT_OBJECT_0) shows that the thread object is still valid.

How are you supposed to tell if a thread has truly exited or not? Yes I know I could set some variables before the function exits but I would think I should be able to query the thread handle to know that it has exited or isn't a valid thread anymore.

David G
  • 94,763
  • 41
  • 167
  • 253

2 Answers2

3

GetExitCodeThread() and WaitForSingleObject() do not lie, so if both are saying the thread is still active then the thread really is still active and has not exited from its control function yet. Check for bugs in your function to make sure it really is exiting when you are expecting it to.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

Both those methods should be working, I would suggest to check though your code for any bugs you may have missed, also I ran into another article here which might be able to help you out.

Point of note:

The best method I know of to end a thread is to make sure the thread does not sleep indefinitely. When it has completed its task have it check a variable.That will tell it whether it's supposed to exit.

i.e

  1. Set Flag
  2. If asleep wake thread
  3. check to see if work is complete
  4. clean up resources
  5. exit
Community
  • 1
  • 1
Joshua Van Hoesen
  • 1,684
  • 15
  • 30