为什么我要使用“Both"?COM 线程模型而不是“免费"? [英] Why would I use "Both" COM threading model instead of "Free"?

查看:12
本文介绍了为什么我要使用“Both"?COM 线程模型而不是“免费"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 这篇文章 如果我使用Both"或Free"线程模型注册我的 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.

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

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"?

推荐答案

双线程模型

将您的组件标记为支持线程模型Both"的主要原因是为了在从单线程单元 (STA) 调用组件时提高性能.

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

如果您将组件标记为 MTA 并且您的组件是在 STA 内创建的,那么您的组件将在单独的 MTA 单元中创建,并且 "由此产生的单元间编组可能会降低性能,足以抵消制作高效、自由线程组件的所有工作".但是,如果您的组件的线程模型被标记为Both",那么它将在 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.

因此,如果您认为您的组件可能在 STA 内被调用(所有 VB6 COM 对象都是 STA),您可能希望将线程模型标记为Both".

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".

一篇关于OLE 线程模型的优秀知识库文章.

A good KB article on OLE Threading Models.

自由线程模型

如果您的组件使用其他标记为免费"的组件,您可能希望使用免费"线程模型.如果您的组件被标记为Both",那么在 STA 和 MTA 中运行的Both"组件之间可能存在过多的单元切换.作为一般规则,尝试创建尽可能靠近调用者的组件(即相同的公寓),同时在所有情况下都能正常运行.

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.

另一种需要将您的组件标记为免费"的情况是它显式阻塞(例如 Thread.Sleep).如果组件被标记为Both"并在 STA 中实例化,则该组件将阻止 STA 消息泵.

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.

其他注意事项和场景

如果您打算在 IIS 中使用该组件,那么还有其他事情需要考虑.对于 IIS,推荐设置Both".主要是为了避免单元线程组件的锁定问题、对 COM+ ObjectContext 的高性能访问以及免费"线程组件使用系统安全上下文的事实(如果您需要访问用户的安全上下文).有关 IIS 的详细信息,请参阅为 IIS 中的组件选择线程模型线程注意事项.

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.

其他要考虑的事情是 COM+ 支持,以及组件在 COM+ 中运行时的行为方式,以及是否传递和存储接口指针.

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.

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

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.

这篇关于为什么我要使用“Both"?COM 线程模型而不是“免费"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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