https://bugs.winehq.org/show_bug.cgi?id=3930
--- Comment #88 from zebra2745@vibzi.net ---
https://www.codeproject.com/Tips/5350589/The-Current-Thread-Handle says:
What to do when you want to use the current thread handle This tip shows a side effect of GetCurrentThread and explains what to do when you need to avoid that.
My Headbump Consider the following example:
------------------------------ snip ------------------------------
DWORD worker(void* ctx) { std::cout << "Thread id: " << GetThreadId((HANDLE)ctx) << std::endl; return 0; }
int main() { HANDLE hMainThread = GetCurrentThread(); std::cout << "Thread id: " << GetThreadId(hMainThread) << std::endl;
HANDLE hThread = CreateThread(NULL, 0, worker, hMainThread, 0, NULL); WaitForSingleObject(hThread, INFINITE); }
------------------------------ snip ------------------------------
If you read the documentation, then this is exactly what it says. Only I didn't stop to think about what that actually meant.
Getting the Real Thread Handle If you want to get a real handle that you can pass to another thread, do this:
------------------------------ snip ------------------------------
HANDLE hMainThread = NULL; if (!DuplicateHandle( GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &hMainThread, 0, FALSE, DUPLICATE_SAME_ACCESS)) { cout << "Error " << GetLastError() << " cannot duplicate main handle." << endl; return GetLastError(); }
------------------------------ snip ------------------------------
Of course, when you do this, you get a HANDLE value that you do need to close when you no longer need it.