在 2019/7/2 下午8:27, Jonathan Wakely 写道: > > What do you mean by "unclosed thread"? If I read it correctly, the MSDN > page > refers to closing a handle (which makes sense), not closing a thread. >
Yes, it meant a thread which has terminated but not deleted due to some handles left open. >> This could also mean that there is no effect way to denote a thread >> uniquely. As a consequence libstdc++ may have to its own bookkeeping >> mechanism. > > As I said in my last mail, libstdc++ does not need a way to denote a > thread uniquely. > At my last glance at the `__gthread_` interfaces, libstdc++ requires thread IDs to be LessThanComparable, which would require retrieval of thread IDs by handle, as in `__gthread_equal()`. More than that, if my previous vision was correct (a terminated thread has no ID associated) then `GetThreadId()` on a thread that has terminated would not return a valid thread ID. Fortunately, this seems not the case: ```c #include <windows.h> #include <stdio.h> DWORD __stdcall ThreadProc(void* pParam) { printf("thread %lu running\n", GetCurrentThreadId()); return 0; } int main(void) { HANDLE hThread = CreateThread(0, 0, ThreadProc, 0, CREATE_SUSPENDED, 0); printf("thread %lu created\n", GetThreadId(hThread)); ResumeThread(hThread); WaitForSingleObject(hThread, INFINITE); printf("thread %lu terminated\n", GetThreadId(hThread)); CloseHandle(hThread); // `hThread` is now invalid; DO NOT PLAY WITH THIS AT HOME! printf("thread %lu closed\n", GetThreadId(hThread)); } ``` This program outputs ```text E:\Desktop>gcc test.c -Wall -Wextra -Wpedantic && a.exe test.c: In function 'ThreadProc': test.c:4:34: warning: unused parameter 'pParam' [-Wunused-parameter] 4 | DWORD __stdcall ThreadProc(void* pParam) | ~~~~~~^~~~~~ thread 9172 created thread 9172 running thread 9172 terminated thread 0 closed E:\Desktop> ``` Despite Microsoft's documentation, the identifier of a thread seems uncollected as long as there are still handles to the thread. So it might be safe to assume that the identifier of an `std::thread` *cannot* be reused before it is `join()`'d or `detach()`'d which closes the handle stored in the `std::thread` object. -- Best regards, LH_Mouse
signature.asc
Description: OpenPGP digital signature