Link to home
Start Free TrialLog in
Avatar of ascot
ascot

asked on

COM threading model question

I am having trouble understanding the COM threading model and in particular when calls to the COM Server will be synchronized and run sequentially and when they will run concurrently.  So I created some programs and ran them in different scenarios that I have described below.

My COM Server is In-Process and uses the  Apartment Threading Model. Below I have described the scenarios and I have the following questions :-

1. Why in Scenario 1 does everything happening sequentially ??  I haven't created any synchronization mechanism to do this so
why aren't my COM objects being executed concurrently by Thread 1 and Thread 2 ?
2. What is the difference between Scenario 1 and Scenario 2 that makes Scenario 2 run concurrently ?
3. Why does Scenraio 4 behave differently from Scenario 3 ?

Scenario 1
------------
1. main does CoInitializeEx(NULL,COINIT_MULTITHREADED);
My understanding> Primary Thread is now in MTA
2. main does CoCreateInstance
My understanding> Because my in-proc object is threading model Appartment, it gets allocated to a new STA ?  
3. main creates Thread 1 passing pointer to the COM object it created using CoCreateInstance.
4. Thread 1 does a CoInitializeEx(NULL,COINIT_MULTITHREADED);  
My understanding> Thread 1 is in MTA
5. main creates Thread 2 passing pointer to the COM object it created.
6. Thread 2 does a CoInitializeEx(NULL,COINIT_MULTITHREADED);  
My understanding> Thread 2 is in MTA
7. Thread 1 and Thread 2 call my COM object.
 
Result : Thread 1 and Thread 2 execute my COM object sequentially
 
----------------------------------------------------------
 
Scenario 2
------------
1. main does CoInitialize(0);
My understanding>Primary Thread is in mainSTA
2. main does CoCreateInstance
My understanding>Because my in-proc object is threading model Appartment, it gets allocated to the mainSTA.
3. main creates Thread 1 passing pointer to the COM object it created.
4. Thread 1 does a CoInitializeEx(NULL,COINIT_MULTITHREADED);  
My understanding>Thread 1 is in MTA
5. main creates Thread 2 passing pointer to the COM object it created.
6. Thread 2 does a CoInitializeEx(NULL,COINIT_MULTITHREADED);  
My understanding>Thread 2 is in MTA
7. Thread 1 and Thread 2 call my COM object and I can see that everything happens concurrently.
 
Result : My COM object is getting run concurrently by Thread 1 and Thread 2
 
-----------------------------------------------------------------------------------------
 
Scenario 3
------------
1. main does CoInitializeEx(NULL,COINIT_MULTITHREADED);  
My understanding>Primary Thread is in MTA
2. main does CoCreateInstance
My understanding>Because my in-proc object is threading model Appartment, it gets allocated to a new STA ?
3. main creates Thread 1 passing pointer to the COM object it created.
4. Thread 1 does a CoInitialize(0);  
My understanding>Thread 1 is in "Thread 1 STA"
5. main creates Thread 2 passing pointer to the COM object it created.
6. Thread 2 does a CoInitialize(0);  
My understanding>Thread 2 is in "Thread 2 STA"
7. Thread 1 and Thread 2 call my COM object
 
Result :  I get the error "The application called an interface that was marshalled for a different thread."
 
-----------------------------------------------------------------------------------------
Scenario 4
------------
1. main does CoInitializeEx(0);
My understanding>Primary Thread is in mainSTA
2. main does CoCreateInstance
My understanding>Because my in-proc object is threading model Appartment, it gets allocated to the mainSTA ?
3. main creates Thread 1 passing pointer to the COM object it created.
4. Thread 1 does a CoInitialize(0);  
My understanding>Thread 1 is in "Thread 1 STA"
5. main creates Thread 2 passing pointer to the COM object it created.
6. Thread 2 does a CoInitialize(0);  
My understanding>Thread 2 is in "Thread 2 STA"
7. Thread 1 and Thread 2 call my COM object
 
Result :  my COM object is getting run concurrently by Thread 1 and Thread 2
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Avatar of ascot
ascot

ASKER

Thanks but they don't help me that much.
I'm trying to understand the interaction between a client and an in-proc object.  For example, one of the links posted says "When multiple clients call an STA object, the calls are automatically queued in the message queue by the transfer of control mechanism used in the STA.".

Well that makes sense fo Scenario 1 but in Scenario 2 where my in-proc object is in the mainSTA, my in-proc object is being called concurrently.  How  is that ?
Avatar of ascot

ASKER

I think the answer probably lies in the rule stated in "Effective COM" of "Don't access raw interface pointers across apartment boundaries" which is what I am effectively doing.
However, I'll close this now as I haven't had a response for a while.
Isn't the threading model determined by the COM object itself not by the thread calling it? If your COM object has been created as an STA then it does not matter how the calling thread invokes the object all requests will be handled using STA? That is my understanding.

No objection
ASKER CERTIFIED SOLUTION
Avatar of OzzMod
OzzMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial