Solved

Is CoMarshalInterThreadInterfaceInStream Thread Specfiic?

Posted on 2004-08-13
3
1,189 Views
Last Modified: 2008-01-16
History:
I am using MS Visual C++ 6.0.
I am creating a WebService wrapper around a legacy VB 6.0 application.
The VB 6.0 application is slow to startup and sign on.  To minimize this overhead, I am attempting to create a pool of objects from which my worker threads can retrieve objects.

Problem:
I have a main thread that creates an instance of my PoolManager class, which in turn creates a pool of COM objects. My PoolManager has a method called getConnection which can locate an available COM object and return a pointer to its interface (actually it returns a pointer to an IStream created from CoMarshalInterThreadInterfaceInStream.)

My worker threads get created when clients connect through the WebService.  Each thread needs to call the PoolManagers getConnection method to get the address of the IStream so it can perform a CoGetInterfaceAndReleaseStream to get a pointer to the COM interface.  

Here is where I am confused:

a) The PoolManager is on thread 1, thus the COM objects were created on thread 1.
b) The Worker is, for example, on thread 2.  
c) The Worker calls PoolManager.getConnecction which executes on thread 2. (assume the code is protected from concurrency issues)  
d) The method getConnection, running on thread 2, calls  CoMarshalInterThreadInterfaceInStream and returns the IStream pointer.  
e) The worker thread continues and calls CoGetInterfaceAndReleaseStream to get an interface pointer.

Questions:
1)      Is it OK that CoMarshalInterThreadInterfaceInStream is called on Thread 2?
2)      If NO, any recommendations on how to avoid this?

Thanks            

0
Comment
Question by:SCDMETA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 11794717
From the sound of it, it should be that you

1) call PoolManager.getConnection in thread 1
2) pass the stream interface pointer to worker thread
3) worker thread calls CoGetInterfaceAndReleaseStream.

You need to marshal the interface in the thread that created it and then pass the result to another thread where it will be unmarshaled.

How are you CoInitialize'ing the threads?  If they are both COINIT_MULTITHREADED, then it really doesn't matter as both threads will be in the same apartment.
0
 
LVL 19

Accepted Solution

by:
drichards earned 500 total points
ID: 11794859
So to directly answer your questions:

>> 1)     Is it OK that CoMarshalInterThreadInterfaceInStream is called on Thread 2?

No

>> 2)     If NO, any recommendations on how to avoid this?

You should call getConnection in thread 1 prior to launching the worker thread and pass the stream interface to the worker thread or if you don't want the connection to be created until worker thread starts, marshal some other pointer to pass to worker thread and change getConnection to receive the unmarshaled interface pointer from the worker thread.

Depending on exactly what you're doing, you might also choose to keep a pool of marshaled interfaces (created on thread 1) and have getConnection unmarshal an interface and work with that.  This would be somewhat more complex as there are issues like cleaning up the marshaled interfaces when you exit and making sure that there were always marshaled interfaces available for getConnection.

0
 
LVL 6

Author Comment

by:SCDMETA
ID: 11796328
I appreciate your quick response.  

I suspected that CoMarshalInterThreadInterfaceInStream had to be called from Thread 1.  
I need to reconsiderd how I am going to approach this problem.  My threads have already been launched by the time I determine if I need the COM interface.

I like your suggestion about keeping a pool of marsheled interface available.  When I free an object back to the pool I can signal Thread 1 to regenerate the IStream.
Thanks for you input.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question