为什么我会使用“两者” COM线程模型而不是“自由”? [英] Why would I use "Both" COM threading model instead of "Free"?

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

问题描述

根据本文如果我注册我的COM对象与两者或自由线程模型,该对象必须完全线程安全。特别地,对全局共享变量的所有访问必须同步,并且对成员变量的所有访问也必须同步。这是很大的努力。



现在我明白,能够注册我的对象使用自由线程模型是有利的,可能值得付出的代价完全线程安全。但为什么我想做所有相同的注册我的对象使用两个线程模型?有什么优势?

解决方案

两个线程模型 p>

将组件标记为支持线程模型两者的主要原因是在从单线程公寓(STA)调用组件时提高性能。



如果您将组件标记为MTA,而您的组件是从STA内创建的,那么您的组件将在单独的MTA公寓中创建,并且产生的公寓间封送可能降低性能,足以抵消构建高效,自由线程组件的所有工作 a>。但是,如果你的组件的线程模型被标记为Both,那么它将被创建在STA对象的公寓内并直接访问。



可以从STA内调用(所有VB6 COM对象都是STA),您可能想将线程模型标记为Both。



OLE线程模型



自由线程模型



如果您的组件使用标记为Free的其他组件,则可能需要使用 。如果您的组件被标记为两者,则在STA中运行的两个组件和MTA之间可能存在过度的公寓切换。一般来说,尽量在所有情况下尽可能靠近调用者(即同一公寓)创建组件。



另一种情况是,你的组件Free是如果它显式阻塞(例如Thread.Sleep)。



其他注意事项和方案。如果组件被标记为两者并在STA中实例化,则该组件将阻止STA消息泵。



如果您计划在IIS中使用组件,那么还有其他事情需要考虑。对于IIS,两者是推荐的设置。主要避免锁定问题与公寓线程组件,执行者访问COM + ObjectContext和免费线程组件使用系统安全上下文(如果您需要访问用户的安全上下文)的事实。有关IIS的更多信息,请参见在IIS中选择组件的线程模型 线程注意事项。



其他需要考虑的事情是COM +支持,以及如果组件在COM +中运行,以及如何传递和存储接口指针,则组件的行为。



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


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

解决方案

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.

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

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