@@ -39,14 +39,20 @@ int win32_pthread_join(pthread_t *thread, void **value_ptr)
{
DWORD result = WaitForSingleObject(thread->handle, INFINITE);
switch (result) {
- case WAIT_OBJECT_0:
- if (value_ptr)
- *value_ptr = thread->arg;
- return 0;
- case WAIT_ABANDONED:
- return EINVAL;
- default:
- return err_win_to_posix(GetLastError());
+ case WAIT_OBJECT_0:
+ if (value_ptr)
+ *value_ptr = thread->arg;
+ /* detach the thread once the join succeeds */
+ CloseHandle(thread->handle);
+ return 0;
+ case WAIT_ABANDONED:
+ /* either thread is not joinable or another thread is waiting on
+ * this, so do not detatch */
+ return EINVAL;
+ case WAIT_FAILED:
+ default:
+ /* the function failed, so do not detach */
+ return err_win_to_posix(GetLastError());
}
}