• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 729
  • Last Modified:

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
0
ascot
Asked:
ascot
  • 3
  • 2
1 Solution
 
ascotAuthor Commented:
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 ?
0
 
ascotAuthor Commented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Julian HansenCommented:
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.

0
 
Julian HansenCommented:
No objection
0
 
OzzModCommented:
Closed, 500 points refunded.
OzzMod
Community Support Moderator (Graveyard shift)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now