wpf 逻辑线程 vs 物理线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18857107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:40:10  来源:igfitidea点击:

logical thread vs physical thread

c#wpfclr

提问by user2788792

I was reading about the System.Threading.DispatcherObject type at this link http://msdn.microsoft.com/en-us/library/ms750441.aspx

我正在阅读有关 System.Threading.DispatcherObject 类型的链接http://msdn.microsoft.com/en-us/library/ms750441.aspx

The article mentions about one to one relationship between logical and physical thread. Here is the snippet from the article:

文章提到了逻辑线程和物理线程之间的一对一关系。这是文章的片段:

During the design phase of WPF, the goal was to move to a single thread of execution, but a non-thread "affinitized" model. Thread affinity happens when a component uses the identity of the executing thread to store some type of state. The most common form of this is to use the thread local store (TLS) to store state. Thread affinity requires that each logical thread of execution be owned by only one physical thread in the operating system, which can become memory intensive

在 WPF 的设计阶段,目标是转移到单线程执行,但非线程“关联”模型。当组件使用执行线程的身份来存储某种类型的状态时,就会发生线程关联。最常见的形式是使用线程本地存储 (TLS) 来存储状态。线程关联要求每个逻辑执行线程只由操作系统中的一个物理线程拥有,这可能会占用大量内存。

Can someone pls. explain what is the difference between logical vs physical thread ?

有人可以请。解释逻辑线程与物理线程之间的区别是什么?

采纳答案by Justin

A local thread is a thread within the runtime. This thread is more lightweight than a full fledged physical thread and are good for lighter weight processes. A physical thread is a thread that a processor context switches to and processes. It has more meta data associated with it as the operating system keeps track of it. There is a lot more detail involved, but that's a quick overview.

本地线程是运行时内的线程。该线程比成熟的物理线程更轻量级,适用于重量较轻的流程。物理线程是处理器上下文切换到并处理的线程。当操作系统跟踪它时,它有更多与之相关的元数据。涉及更多细节,但这是一个快速概述。

The physical thread in that case would contain these logical "virtual threads" internally.

在这种情况下,物理线程将在内部包含这些逻辑“虚拟线程”。

回答by Alois Kraus

The CLR of .NET 2.0 did plan to have fiber support as .NETThread for SQL CLR. It did never make it into the product but it could be that in a distant future the CLR will support it. Hence this big red thread affinity warning.

.NET 2.0 的 CLR 确实计划将Fiber 支持作为SQL CLR 的.NET线程。它从未进入产品,但在遥远的将来 CLR 可能会支持它。因此,这个大红色线程关联警告。

The OS does supportfibers natively but they are quite hard to use correctly since you are switching in and out your stack in user mode to let other code run on the same physical thread. This does eliminate costly context switches which can make sense in some high performance scenarios. To use fibers as abstractions is hardly a good idea since you cannot call any third party library since you do not know if the api does assume thread affinity or not.

操作系统不支持纤维本身,但它们都很难使用正确的,因为你是在和出在用户模式下的堆栈切换,让同一个物理线程上的其他代码运行。这确实消除了成本高昂的上下文切换,这在某些高性能场景中是有意义的。使用纤程作为抽象并不是一个好主意,因为您不能调用任何第三方库,因为您不知道 api 是否确实假定线程关联。

The cost of context switches in todays multi core machines is much less than it was several Windows versions and processor generations earlier. I doubt that you will gain much with fibers in a real world application. Besides this the chip makers are working hard to make the cost of context switches even less. There are designs discussedto make it as cheap as one CPU cycle.

当今多核机器中上下文切换的成本远低于早期的几个 Windows 版本和处理器世代。我怀疑您是否会在实际应用中使用 Fiber 获得很多好处。除此之外,芯片制造商正在努力降低上下文切换的成本。讨论了一些设计,使其与一个 CPU 周期一样便宜。

To make a long story short:

使长话短说:

It could happen that the CLR Thread object is not a physical OS thread if you host the CLR by yourself and use a not (yet) existing feature of the CLR hosting APIs.

如果您自己托管 CLR 并使用 CLR 托管 API 的(尚未)现有功能,则 CLR 线程对象可能不是物理操作系统线程。

As far as I do know the Window message pump system will stay there even in Windows 8 where each window has thread affinity to the creating thread which must also pump the window messages.

据我所知,即使在 Windows 8 中,Window 消息泵系统也将保留在那里,其中每个窗口都与创建线程具有线程关联,创建线程也必须泵送窗口消息。