为什么使用消息队列创建线程?
这是特定于Windows的问题。
根据 Microsoft文档 ,,,,
该系统为每个GUI线程维护一个系统消息队列和一个特定于线程的消息队列。为了避免为非GUI线程创建消息队列的开销,最初创建所有线程而没有消息队列。仅当线程将其第一个调用到特定用户功能之一时,系统才会创建特定于线程的消息队列;没有GUI函数调用导致创建消息队列。
到目前为止,我相信所有线程最初都是在没有消息队列的情况下创建的。今天,我用因为说
如果指定的线程没有消息队列,则功能会失败。
但是,PostThreadMessageW总是在创建后立即为所有线程返回1(true)。
DWORD dwThreadID = GetCurrentThreadId();
BOOL bResult = PostThreadMessageW(dwThreadID, WM_QUIT, 123, 456);
为什么会发生这种情况?
This is windows specific question.
According to microsoft docs,
The system maintains a single system message queue and one thread-specific message queue for each GUI thread. To avoid the overhead of creating a message queue for non–GUI threads, all threads are created initially without a message queue. The system creates a thread-specific message queue only when the thread makes its first call to one of the specific user functions; no GUI function calls result in the creation of a message queue.
So far I believed on that all threads are created initially without a message queue. Today, I tested this with PostThreadMessageW because it is stated that
The function fails if the specified thread does not have a message queue.
However, PostThreadMessageW always returns 1 (TRUE) for all threads right after creation.
DWORD dwThreadID = GetCurrentThreadId();
BOOL bResult = PostThreadMessageW(dwThreadID, WM_QUIT, 123, 456);
Why is this happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如书面,这个问题没有很大的意义。假设文档
是正确的,所讨论的代码不适合反驳此语句。这是说明缺乏原因的注释代码:
我不知道文档是否仍然准确(具体来说,该部分解释了如何根据需要创建特定于线程的消息队列)。尽管如此,该代码并没有质疑其准确性。
POSTTHREADMESSAGEW()
带有当前线程的线程ID,未失败的位置在规范之内(忽略错误条件,例如error_not_enough_enough_quota
)。As written, the question doesn't make a whole lot of sense. Under the assumption that the following passage in the documentation
is correct, the code in question is unsuitable to refute this statement. Here is the annotated code to illustrate the lack of reason:
I don't know whether the documentation is still accurate (specifically, the part that explains how thread-specific message queues are created on demand). Still, the code does nothing to question its accuracy.
PostThreadMessageW()
with a thread ID of the current thread not failing is within specification (ignoring error conditions such asERROR_NOT_ENOUGH_QUOTA
).