Solved

Is CoMarshalInterThreadInterfaceInStream Thread Specfiic?

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

14 Experts available now in Live!

Get 1:1 Help Now