Solved

Is CoMarshalInterThreadInterfaceInStream Thread Specfiic?

Posted on 2004-08-13
3
1,170 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

739 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