windows 为什么我要使用“Both”COM 线程模型而不是“Free”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1824397/
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
Why would I use "Both" COM threading model instead of "Free"?
提问by sharptooth
According to this articleif 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”或“Free”线程模型注册我的 COM 对象,该对象必须是完全线程安全的。具体来说,所有对全局共享变量的访问必须同步,对成员变量的所有访问也必须同步。这是很多努力。
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”线程模型注册我的对象呢?会有什么好处?我如何在“两者”和“免费”之间做出选择?
回答by Randy supports Monica
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).
将组件标记为支持线程模型“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.
如果您将您的组件标记为 MTA 并且您的组件是从 STA 内创建的,那么您的组件将在单独的 MTA 单元中创建,并且“由此产生的单元间封送可能会降低性能,足以抵消投入到高效、自由线程组件”。但是,如果您的组件的线程模型被标记为“Both”,那么它将在 STA 对象的单元内创建并直接访问。
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".
因此,如果您认为您的组件可以从 STA 中调用(所有 VB6 COM 对象都是 STA),您可能希望将线程模型标记为“Both”。
A good KB article on OLE Threading Models.
一篇关于OLE 线程模型的优秀知识库文章。
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.
如果您的组件使用其他标记为“免费”的组件,您可能希望使用“免费”线程模型。如果您的组件被标记为“Both”,那么在 STA 和 MTA 中运行的“Both”组件之间可能会有过多的单元切换。作为一般规则,尝试创建组件尽可能靠近调用者(即相同的公寓),同时在所有场景下都能正常运行。
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.
另一种需要将您的组件标记为“空闲”的情况是它是否显式阻塞(例如 Thread.Sleep)。如果该组件被标记为“Both”并在 STA 中实例化,则该组件将阻塞 STA 消息泵。
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 IISfor more info about IIS threading considerations.
如果您计划在 IIS 中使用该组件,则还需要考虑其他事项。对于 IIS,推荐设置“Both”。主要是为了避免 Apartment 线程组件的锁定问题、对 COM+ ObjectContext 的高性能访问以及“免费”线程组件使用系统安全上下文的事实(如果您需要访问用户的安全上下文)。有关 IIS 线程注意事项的详细信息,请参阅为 IIS 中的组件选择线程模型。
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+ 中运行时的行为方式以及是否传递和存储接口指针。
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.
一篇优秀的文章是COM+ 应用程序中的 COM 线程和应用程序架构。它以 COM+ 为重点,但也讨论了 COM。对于您的问题,请阅读标题为“线程模型建议”的部分。Microsoft 已删除原始文章,因此我将链接到副本。