消息循环如何使用线程? [英] How does the message loop use threads?

查看:126
本文介绍了消息循环如何使用线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑,想知道如果我被误解,在一个单独的文章,我被告知新线程只有创建时,你明确做出他们。C ++程序是默认单线程。当我打开我的程序,没有显式地创建新线程在ollydbg我注意到多次,有2个线程运行。我想了解消息循环如何工作,而不停止执行,我得到的解释是非常不足以解释它的工作原理。

I'm somewhat confused and wondering if I've been misinformed, in a separate post I was told "New threads are only created when you make them explicitly. C++ programs are by default single threaded." When I open my program that doesn't explicitly create new threads in ollydbg I noticed multiple times that there are often 2 threads running. I wanted to understand how the message loop works without stopping up execution, the explanation I got was very insufficient at explaining how it works.

消息循环是否创建一个新线程或占用主线程?如果它采取主线程,它执行后,一切执行后,无论代码顺序?如果它不这样做,但仍然占用主线程,它产生一个新的线程,以便程序可以执行,而不是陷入消息循环?

Does the message loop create a new thread or does it take up the main thread? If it takes the main thread does it do so after everything else has been executed regardless of code order? If it doesn't do this but still takes up the main thread does it spawn a new thread so that the program can execute instead of getting stuck in the message loop?

编辑:通过实验解决我的大部分问题。消息循环占用主线程和代码后的任何代码:

Solved most of my questions with experimentation. The message loop occupies the main thread and any code after the code:

while (GetMessage (&messages, NULL, 0, 0))
{
    TranslateMessage(&messages);
    DispatchMessage(&messages);
}
return messages.wParam;

除非执行特殊操作,否则将无法执行,因为程序停留在消息中循环。在执行的窗口过程中设置无限循环会导致程序崩溃。

Will not execute unless something special is done to cause it to execute because the program is stuck in the message loop. Putting an infinite loop in a window procedure that gets executed causes the program to crash. I still don't understand the mystery of the multiple threads when in olly to the degree I would prefer though.

推荐答案

也许开始的地方是意识到消息循环不是这样的事情;

Perhaps the place to start is to realize that "the message loop" isn't a thing as such; it's really just something that a thread does.

窗口中的线程通常分为两类:拥有UI的线程和做后台工作的线程(例如网络操作)。

Threads in windows generally fall into one of two categories: those that own UI, and those that do background work (eg network operations).

一个简单的UI应用程序通常只有一个线程,它是一个UI线程。为了UI的工作,线程需要等待有一些输入来处理(鼠标点击,键盘输入等),处理输入(例如更新状态和重绘窗口),然后回到等待更多输入。这种等待输入,处理,重复的整个行为是消息循环。 (在这个阶段还值得提到的是消息队列:每个线程都有自己的输入队列,用于存储线程的输入消息;线程等待输入的行为实际上是检查队列中是否有任何内容如果线程正在积极地处理这种输入,它也被称为抽取消息。

A simple UI app typically has just one thread, which is a UI thread. For the UI to work, the thread needs to wait until there's some input to handle (mouse click, keyboard input, etc), handle the input (eg. update the state and redraw the window), and then go back to waiting for more input. This whole act of "wait for input, process it, repeat" is the message loop. (Also worth mentioning at this stage is the message queue: each thread has its own input queue which stores up the input messages for a thread; and the act of a thread "waiting for input" is really about checking if there's anything in the queue, and if not, waiting till there is.) In win32 speak, if a thread is actively processing input this way, it's also said to be "pumping messages".

一个典型的简单的Windows应用程序的主线代码将首先做基本的初始化,创建主窗口,然后做等待输入和进程的消息循环。它通常直到用户关闭主窗口,此时线程退出循环,并继续执行后面的代码,这通常是清理代码。

A typical simple windows app's mainline code will first do basic initialization, create the main window, and then do the wait-for-input-and-process-it message loop. It does this usually until the user closes the main window, at which point the thread exits the loop, and carries on executing the code that comes afterwards, which is usually cleanup code.

Windows应用程序中的一个常见架构是拥有一个主UI线程 - 通常这是主线程 - 它创建并拥有所有的UI,并且有一个消息循环它为线程创建的所有UI分派消息。如果应用程序需要做一些可能阻塞的事情,例如从套接字读取,工作线程通常用于此目的:你不想让UI线程阻塞(例如,当等待来自套接字的输入时) ,因为它不会在这段时间内处理输入,并且UI会最终无响应。

A common architecture in windows apps is to have a main UI thread - usually this is the main thread - and it creates and owns all the UI, and has a message loop that dispatches messages for all of the UI that the thread created. If an app needs to do something that could potentially block, such as reading from a socket, a worker thread is often used for that purpose: you don't want the UI thread to block (eg. while waiting for input from a socket), as it wouldn't be processing input during that time and the UI would end up being unresponsive.

您可以编写一个应用程序,其中有多个UI线程 - 并且每个创建窗口的线程都需要它自己的消息循环 - 但它是一个相当高级的技术,并不是所有对大多数基本的应用程序有用。

You could write an app that had more than one UI thread in it - and each thread that creates windows would then need its own message loop - but it's a fairly advanced technique and not all that useful for most basic apps.

其他线程正在看到可能是由Windows创建的一些帮助线程来执行后台任务;并且在大多数情况下,您可以忽略它们。例如,如果你初始化COM,windows可能会创建一个工作线程来管理一些COM内部的东西,并且它也可能创建一些不可见的HWND。

The other threads you are seeing are likely some sort of helper threads that are created by Windows to do background tasks; and for the most part, you can ignore them. If you initialize COM, for example, windows may end up creating a worker thread to manage some COM internal stuff, and it may also create some invisible HWNDs too.

这篇关于消息循环如何使用线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆