Currently(C++17) you can't get native_handle
from std::this_thread
The most possible interface might be std::this_thread::native_handle()
. But not std::this_thread::get_id().native_handle();
by @Howard
Since Win/Linux/MacOS implement thread
and thread::id
differently: (below is informal pseudo code)
- On Linux
native_handle
is stored at thread._M_id(of type id)._M_thread.
- On Windows
native_handle
is stored at thread._Thr(of type _Thrd_t, not of type id)._Hnd.
- On MacOS
native_handle
is stored at thread.__t_.
As you can see only in Linux source there is native_hanlde
object implemented in thread::id
structure. Thus on Win/MacOS you can't get the native_handle
from an id
object.
Finally, if your code runs only in Linux, there is a dirty trick to get native_handle
from this_thread
which I will never recommend:
auto thread_id = std::this_thread::get_id();
auto native_handle = *reinterpret_cast<std::thread::native_handle_type*>(&thread_id);