为什么使用消息队列创建线程?

发布于 2025-02-14 00:47:21 字数 893 浏览 0 评论 0原文

这是特定于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪少女 2025-02-21 00:47:21

如书面,这个问题没有很大的意义。假设文档

仅当线程对特定用户功能之一[...]。

是正确的,所讨论的代码不适合反驳此语句。这是说明缺乏原因的注释代码:

DWORD dwThreadID = GetCurrentThreadId();
// Call a message-specific USER function on the current thread
BOOL bResult = PostThreadMessageW(dwThreadID, WM_QUIT, 123, 456);
// Ask a question on SO why the current thread maintains a message queue

我不知道文档是否仍然准确(具体来说,该部分解释了如何根据需要创建特定于线程的消息队列)。尽管如此,该代码并没有质疑其准确性。 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

The system creates a thread-specific message queue only when the thread makes its first call to one of the specific user functions [...].

is correct, the code in question is unsuitable to refute this statement. Here is the annotated code to illustrate the lack of reason:

DWORD dwThreadID = GetCurrentThreadId();
// Call a message-specific USER function on the current thread
BOOL bResult = PostThreadMessageW(dwThreadID, WM_QUIT, 123, 456);
// Ask a question on SO why the current thread maintains a message queue

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 as ERROR_NOT_ENOUGH_QUOTA).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文