为什么我要使用“两者” COM 线程模型而不是“免费”?

发布于 2024-08-13 20:29:14 字数 322 浏览 0 评论 0原文

根据本文 如果我使用“两者”或“自由”线程模型注册 COM 对象,则该对象必须是完全线程安全的。具体来说,所有对全局共享变量的访问都必须同步,所有对成员变量的访问也必须同步。这需要付出很大的努力。

现在我明白,能够将我的对象注册为使用“免费”线程模型是有利的,并且可能值得付出使其完全线程安全的代价。但为什么我想要做同样的事情并使用“两者”线程模型来注册我的对象呢?会有什么好处? “两者”和“免费”之间如何选择?

According to this article if I register my COM object with either "Both" or "Free" threading model that object must be completely thread-safe. Specifically all accesses to global shared variables must be synchronized and all accesses to member variables must also be synchronized. That's a lot of effort.

Now I understand that being able to register my object as using "Free" threading model is advantageous and might be worth paying the price of making it completely thread-safe. But why would I want to do all the same and register my object using "Both" threading model instead? What would be the advantage? How do I choose between "Both" and "Free"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我很OK 2024-08-20 20:29:14

双线程模型

将组件标记为支持线程模型“双”的主要原因是为了提高从单线程单元 (STA) 调用组件时的性能。

如果您将组件标记为 MTA 并且您的组件是从 STA 中创建的,那么您的组件将在单独的 MTA 单元中创建,并且 "由此产生的单元间封送可能会降低性能,足以抵消为制作高效、自由线程组件而投入的所有工作"。但是,如果组件的线程模型标记为“两者”,那么它将在 STA 对象的单元内创建并直接访问。

因此,如果您认为您的组件可以从 STA 内调用(所有 VB6 COM 对象都是 STA),您可能需要将线程模型标记为“两者”。

关于 OLE 线程模型 的一篇很好的知识库文章。

自由线程模型

如果您的组件使用标记为“自由”的其他组件,您可能需要使用“自由”线程模型。如果您的组件被标记为“两者”,那么在 STA 和 MTA 中运行的“两者”组件之间可能会出现过多的单元切换。作为一般规则,尝试创建尽可能靠近调用者的组件(即同一公寓),同时在所有情况下都能正常运行。

另一种需要将组件标记为“空闲”的情况是它显式阻塞(例如 Thread.Sleep)。如果组件被标记为“两者”并在 STA 中实例化,则该组件将阻止 STA 消息泵。

其他注意事项和方案

如果您计划在 IIS 中使用该组件,那么还需要考虑其他事项。对于 IIS,建议设置“两者”。主要是为了避免 Apartment 线程组件的锁定问题、对 COM+ ObjectContext 的高性能访问以及“自由”线程组件使用系统安全上下文这一事实(如果您需要访问用户的安全上下文)。有关 IIS 的详细信息,请参阅为 IIS 中的组件选择线程模型线程注意事项。

其他需要考虑的事项包括 COM+ 支持、组件在 COM+ 中运行时的行为方式以及是否传递和存储接口指针。

一篇优秀的文章是 COM+ 应用程序中的 COM 线程和应用程序体系结构。它重点关注 COM+,但也讨论了 COM。对于您的问题,请阅读标题为“线程模型建议”的部分。 Microsoft 已删除原始文章,因此我链接到副本。

Both Threading Model

The main reason for marking your component as supporting threading model "Both" is for performance improvements when the component is being called from a Single Threaded Apartment (STA).

If you mark your component as MTA and your component is created from within a STA then your component will be created in a separate MTA apartment and the "resultant inter-apartment marshaling might degrade performance enough to negate all the work put into making an efficient, free-threaded component". However, if your component's threading model is marked as "Both" then it will be created inside the apartment of the STA object and accessed directly.

So if you think your component may be called from within a STA (all VB6 COM objects are STA) you might want to mark the threading model as "Both".

A good KB article on OLE Threading Models.

Free Threading Model

You might want to use a "Free" thread model if your component uses other components that are marked as "Free". If your component was marked as "Both" then there could be excessive apartment switching between the "Both" component running in the STA and the MTA. As a general rule, try to create the component as close to the caller as possible (i.e. same apartment) while functioning properly under all scenarios.

Another situation that would warrant marking your component as "Free" is if it explicitly blocks (e.g. Thread.Sleep). If the component is marked as "Both" and instantiated in a STA then the component would block the STA message pump.

Other Considerations and Scenarios

If you are planning on using the component in IIS, then there are other things to consider. For IIS, "Both" is the recommended setting. Mainly to avoid locking issues with Apartment threaded components, performant access to COM+ ObjectContext and the fact that "Free" threaded components use the system security context (if you require access to the user's security context). See Selecting a Threading Model for Components in IIS for more info about IIS threading considerations.

Other things to consider are COM+ support and how your components behave if they are run in COM+ and whether interface pointers are passed and stored.

An excellent article is COM Threading and Application Architecture in COM+ Applications. It has a COM+ focus but also discusses COM. For your question, read the section entitled "Threading Model Recommendations". Microsoft has removed the original article so I'm linking to a copy.

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