質問

If I have a Win32 thread, I reference it via a handle and can also obtain it's thread ID.

The handle remains valid and usable until I close the handle, even if the thread has been terminated. (Otherwise, one couldn't use e.g. GetExitCodeThread

It seems however that the docs fail to mention consistently what "happens" to the thread ID after a thread has terminated, but as long as anyone still holds a thread handle to the terminated thread.

Thread Handles and Identifiers states that

The identifiers are valid from the time the thread is created until the thread has been terminated.

When we then look at the GetThreadId API, we can wonder what that would be supposed to return for a terminated thread if the ID would only be valid until a thread terminates. (And also there's a user comment on this site: "Is not correct. As long as there is an open handle to the thread, it's thread ID is still valid and unique." which does answer this here question with yes, but without more context I'm wary.)

Also, I found this snippet in an answer here on SO:

The ID is the unique numeric idetifier of the thread running in the system. A thread handle, like any kernel object handle, can be seen as a special type of reference counted pointer to the kernel object.

If that is correct and the thread ID is indeed the ID of the kernel object, then it seems it would necessitate that this ID remains valid as long as any handles are open (because the kernel object needs to remain there to be queried).

Is my reasoning sound? Is the thread ID valid as long as any handles are open? Is there any MS docs or authoritative book that explicitly states one or the other?

役に立ちましたか?

解決

Note that:

So an identifier can only be reused after last thread handle is closed (that is, your assumption is correct).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top