Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would I use "Both" COM threading model instead of "Free"?

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

like image 586
sharptooth Avatar asked Dec 01 '09 06:12

sharptooth


People also ask

What is COM threading model?

A process is a collection of virtual memory space, code, data, and system resources, while a thread is code that is to be serially executed within a process. A processor executes threads, not processes, so each application has at least one process and one thread.

What does free-threaded mean?

a free-threaded component supports a multithreaded environment. It would obviously have to be thread-safe in order to achieve this. The opposite would be a component that is designed for an STA, ie. only allows calls from one particular thread.

What is multi threaded apartment?

In a multithreaded apartment model, all the threads in the process that have been initialized as free-threaded reside in a single apartment. Therefore, there is no need to marshal between threads. The threads need not retrieve and dispatch messages because COM does not use window messages in this model.

What is single threaded apartment?

There are two types of apartments: single-threaded apartments, and multithreaded apartments. Single-threaded apartments consist of exactly one thread, so all COM objects that live in a single-threaded apartment can receive method calls only from the one thread that belongs to that apartment.


1 Answers

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.

like image 135
Randy supports Monica Avatar answered Oct 26 '22 07:10

Randy supports Monica