Why is COM + ignoring the flat slicing model?

I have a COM STA component that is being pushed into a COM + application. The client creates multiple instances of the class in this component and calls their methods in parallel. The class is registered correctly - "ThreadingModel" for the corresponding class identifier "Apartment".

I see multiple calls to the same method of the same class that are executed in parallel within the component - in the actual code of the component. They run in the same process, but in different threads.

What's happening? COM + ignores streaming model? Should the STA model only allow one call at a time?

+1


a source to share


4 answers


To avoid confusion, I will not use the term "object" in this answer. Use "class" and "instance" instead. I'm sure we all understand the difference between the two.

Marking your COM class with the ThreadingModel "Apartment" means that its instances will be loaded into the STA. The process of creating these instances will determine whether they are all part of the same STA or separate STAs.

As you found, COM + loaded multiple instances into separate STAs.



The guarantee you get with STA is that a single instance will never be accessed by multiple threads at the same time. Separate instances of the same class, if loaded into separate STAs, can be accessed simultaneously by different threads.

Thus, STA is truly a way to protect your instance data. Not your class data. Any "general" or "static" data in your COM code must be protected by you.

+1


a source


No, not at all. STA literally means "Single Threaded Apartment" which also means that only one thread can run in an apartment. Now the question is, what is an apartment? An apartment is a logical space within a process, and its implementation can vary from structure to structure. Microsoft implements flats as threads due to which STA (in Microsoft COM Context) is converted to single threaded thread, i.e. there can be multiple flats / threads, but each flat / thread will be single threaded in case of STA.



You can generalize this to the MTA yourself. From what I said above, MTA is a multi-threaded thread in a COM context.

+1


a source


STA makes sure that your object is only accessible from one particular thread - no shared variable protection is required.

I remember there was a special mode for VB6 (I don't remember what it was named): you can allow COM + to create multiple STAs, each using a dedicated object. However, the variables of these objects were treated as thread-local storage, so although multiple instances of your COM class are accessed from multiple threads, no variables are exchanged. Is it possible that you are using this feature?

+1


a source


Have you transferred the object to objects that live in another apartment? If so, did you need to marshal the interface before you did? Did you manage to complete the free threaded marker pen?

Roughly speaking, if you are passing an interface to your object to an object in another apartment (thread), then you have to make sure that the interface marshal . If you don't, you might find that your object can be called freely from objects in another apartment, since they don't call through a proxy that handles the call correctly.

All calls to an object must be made to its thread (within its apartment). It is forbidden to call an object directly from another thread; using objects in this loose sequence can cause problems for applications. The consequence of this rule is that all pointers to objects must be when passing between apartments. COM provides the following two functions for this purpose:

* CoMarshalInterThreadInterfaceInStream marshals an interface into a stream object that is returned to the caller.
* CoGetInterfaceAndReleaseStream unmarshals an interface pointer from a stream object and releases it.

      

These functions complete calls to CoMarshalInterface and CoUnmarshalInterface functions that require the use of the MSHCTX_INPROC flag.

+1


a source







All Articles