Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^5: Threads and signals in Windows

by BrowserUk (Pope)
on Mar 10, 2013 at 10:15 UTC ( #1022657=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1022657]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2017-05-26 17:11 GMT
Find Nodes?
    Voting Booth?