Search code examples
c++windowsmultithreadingmsys

<Windows>Why does std::thread::native_handle return a value of type 'long long unsigned int' instead of void* (a.k.a. HANDLE)?


I need to suspend a thread on windows via Windows SDK on msys. I tried something like

std::thread thread(somefunction, someparameters);
HANDLE handle=thread.native_handle();
SuspendThread(handle);

But gcc told me the return value of native_handle() is 'long long unsigned int' but not void*. So I tried

HANDLE handle=reinterpret_cast<HANDLE>(thread.native_handle());

But it does not work because when I called GetLastError() I received the error code 6 which means the handle is invalid. What should I do?


Solution

  • The returned "handle" is the thread id not the HANDLE as returned by CreateThread.

    You need to use OpenThread to get a handle from the id.