C++11’s standard library contains <thread>
which allows threads to be created. However, the Windows api requires an ID for some functions (PostThreadMessage, namely). How can I get it?
remark: std::thread::get_id()
doesn’t seem to work:
PostThreadMessage(m_thread->get_id(), WM_QUIT, 0, 0);
e:\documents\khook\khooker\hook_runner.cpp(129): error C2664: 'PostThreadMessageW' : cannot convert parameter 1 from 'std::thread::id' to 'DWORD'
The pthread_self() function is used to get the ID of the current thread. This function can uniquely identify the existing threads.
Thread Id is a long positive integer that is created when the thread was created. During the entire lifecycle of a thread, the thread ID is unique and remains unchanged. It can be reused when the thread is terminated.
To create a thread, the Windows API supplies the CreateThread( ) function. Each thread has its own stack (see thread vs processes). You can specify the size of the new thread's stack in bytes using the stackSize parameter which is the 2nd argument of CreateThread( ) function in the example below.
Creates a thread to execute within the virtual address space of the calling process. To create a thread that runs in the virtual address space of another process, use the CreateRemoteThread function.
Use the member function native_handle()
. It provides the native thread handle. Then you can call GetThreadId()
on it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With