Solved

Is CoMarshalInterThreadInterfaceInStream Thread Specfiic?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now