libimobiledevice / libimobiledevice-glue

A library with common code used by libraries and tools around the libimobiledevice project
GNU Lesser General Public License v2.1
91 stars 70 forks source link

Correctly correspond to pthread_ kill #1

Closed AiXanadu closed 2 years ago

AiXanadu commented 3 years ago

Sometimes WaitForSingleObject cannot be returned due to thread exceptions. TerminateThread can force the thread to end.

nikias commented 2 years ago

Sorry but this doesn't work as expected. I tried and it immediately killed the thread instead of just checking it's status. If have also seen people trying to use GetExitCodeThread to test if a thread is still running, but actually MS is pointing out not to do it, and instead suggest to use WaitForSingleObject with 0 duration, what the current code is already doing:

Important The GetExitCodeThread function returns a valid error code defined by the application only after the thread terminates. Therefore, an application should not use STILL_ACTIVE (259) as an error code. If a thread returns STILL_ACTIVE (259) as an error code, applications that test for this value could interpret it to mean that the thread is still running and continue to test for the completion of the thread after the thread has terminated, which could put the application into an infinite loop. To avoid this problem, callers should call the GetExitCodeThread function only after the thread has been confirmed to have exited. Use the WaitForSingleObject function with a wait duration of zero to determine whether a thread has exited.

(see https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getexitcodethread)