http://www.perlmonks.org?node_id=1022657


in reply to Re^4: Threads and signals in Windows
in thread Threads and signals in Windows

Is it OK with “Sleep(0)”, or must it be a non zero parameter value?

The problem here is the assumption that yielding the processor will ensure that the terminated thread has had time to fully clean up before this thread will get another timeslice and exit. If the system is busy, many other threads in the system are eligible to run -- especially if they are higher priority -- then the callee of TerminateThread() might easily get several timeslices before the thread actually ends.

The correct (or maybe just better) thing to do, would be to pass a known exit code value on the TerminateThread() call and then loop checking the thread exit code until it returns that value rather than STILL_ACTIVE. Eg. Something like this:

TerminateThread( hProcess, 123456789 ); Sleep( 0 ); GetExitCodeThread( hProcess, &exitCode ); while( exitCode != 123456789 ) { Sleep(0); GetExitCodeThread( hProcess, &exitCode ); }

Once GetExitCodeThread() returns the known value, you should be pretty certain that the thread has actually terminated.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.