Solved

Is CoMarshalInterThreadInterfaceInStream Thread Specfiic?

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting to C# also VS2015 5 128
C++ get user from AD  (VS6) 7 75
ASP.net build a IF/Then Walkthrough Guide 1 214
basic hardware to learn oop advanced design patterns 3 96
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

803 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