[英]How to pass std::thread as thread id in PostThreadMessage?
How could I pass the id of a std::thread
thread as the id into PostThreadMessage
?如何将
std::thread
线程的 id 作为 id 传递给PostThreadMessage
?
Like suppose, I have a thread:就像假设一样,我有一个线程:
// Worker Thread
auto thread_func = [](){
while(true) {
MSG msg;
if(GetMessage(&msg, NULL, 0, 0)) {
// Do appropriate stuff depending on the message
}
}
};
std::thread thread_not_main = std::thread(thread_func);
And then I want to send a message from my main thread to the above thread so that I can handle the message in a non-expensive way.然后我想从我的主线程向上面的线程发送一条消息,这样我就可以以一种不昂贵的方式处理这条消息。 So as to not interrupt the main thread.
以免中断主线程。
Like:喜欢:
// Main Thread
while(true) {
MSG msg;
while(GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
if(msg.message == WM_PAINT) {
PostThreadMessage(); // How do I pass the thread id into the function?
} else {
DispatchMessage(&msg);
}
}
}
The summary of the problem is that PostThreadMessage
requires a thread-id to be passed in as a parameter, now std::thread::get_id
doesn't provide it in a " DWORD
convertible format" .问题的总结是
PostThreadMessage
需要一个线程 ID作为参数传入,现在std::thread::get_id
没有以" DWORD
可转换格式"提供它。 So then I can't pass the thread's id as a parameter.那么我不能将线程的 id 作为参数传递。
My question is: How would I pass the thread id as a parameter to PostThreadMessage
?我的问题是:如何将线程 id 作为参数传递给
PostThreadMessage
?
You can get the underlying, "Windows-style" thread handle for a std::thread
object by calling its native_handle()
member function .您可以通过调用其
native_handle()
成员 function来获取std::thread
object 的底层“Windows 样式”线程句柄。 From that, you can retrieve the thread's ID by calling the GetThreadId
WinAPI function , and passing that native handle as its argument.从那里,您可以通过调用
GetThreadId
WinAPI function并将该本机句柄作为其参数传递来检索线程的 ID。
Here's a short code snippet that may be what you'd want in the case you've outlined:这是一个简短的代码片段,在您概述的情况下可能是您想要的:
auto tHandle = thread_not_main.native_handle(); // Gets a "HANDLE"
auto tID = GetThreadId(tHandle); // Gets a DWORD ID
if (msg.message == WM_PAINT) {
PostThreadMessage(tID, msg, wParam, lParam);
}
else {
DispatchMessage(&msg);
}
//...
std::thread
does not expose the Win32 Thread ID that PostThreadMessage()
wants. std::thread
不会公开PostThreadMessage()
想要的 Win32 线程 ID。
The best way to handle this issue is to call GetCurrentThreadId()
inside of your thread function itself, saving the result to a variable that you can then use with PostThreadMessage()
when needed.处理此问题的最佳方法是在线程 function 本身内部调用
GetCurrentThreadId()
,将结果保存到一个变量中,然后在需要时可以与PostThreadMessage()
一起使用。
For example:例如:
struct threadInfo
{
DWORD id;
std::condition_variable hasId;
};
auto thread_func = [](threadInfo &info){
info.id = GetCurrentThreadId();
info.hasId.notify_one();
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
// Do appropriate stuff depending on the message
}
};
...
threadInfo info;
std::thread thread_not_main(thread_func, std::ref(info));
info.hasId.wait();
...
PostThreadMessage(info.id, ...);
Solution:解决方案:
You can use the native_handle()
member function to get the underlying platform-specific handle of the thread, and then pass that to PostThreadMessage()
:您可以使用
native_handle()
成员 function 来获取线程的底层平台特定句柄,然后将其传递给PostThreadMessage()
:
std::thread thread_not_main = std::thread(thread_func);
...
PostThreadMessage(thread_not_main.native_handle(), WM_PAINT, 0, 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.