tags:

views:

42

answers:

2

Hi all

We are given in-proc-server (.dll) with threading model "Both".i noticed the threading model in registry editor.as we know com object created with "Both" will take the threading model of the thread in which the object is created. i instantiated the object in a STA thread to make sure call to com object should go through only one thread. i was invoking the methods of this objects from child thread in a serialized way where object is actually created in main(STA) thread

will it make any side effects?

I did n't get any errors and executes fine when i used the object in child thread without any marshalling(GIT) .

now my doubt is how come it is happening.i didn't get any staright way answer for this when i google. please give me a brief description abt this

as per my understanding marshalling of an object is require if the object supports "STA" is it so? here our object supports "Both".

A: 

See this very good explanation. Whether marshalling will be used will depend on whether the object and the caller are in the same apartment. If I get your words right you call CoInitializeEx() to place a thread into STA, then you call CoCreateInstance() from that thread - the object will be created in the same STA, so the original creator thread will communicate with it directly. How the other thread will use that object will depend on apartment configuration. It's unclear from your question whether the other thread calls CoInitializeEx() and how the object pointer is thransferred to it. If you just pass a raw pointer then there're no means for how marshalling would turn on.

When you pass a pointer to another thread you might encounter real problems. Part two of the above article says that you should never pass raw pointers between apartments. However looks like your case is an exception to that rule. The COM class is marked to have Both threading model, so it must be fully thread-safe and therefore its methods can be called from multiple threads simultaneously. I'm not sure about this, but looks like you're safe.

sharptooth
>Whether marshalling will be used will depend on whether the object and the caller are in the same apartment<<<<<<In our case object is created in main thread with CoinitializeEx(NULL,COINIT_APARTMENTTHREADED) and using object methods will happen from child thread without any marshalling.the child thread is ininitialized with CoinitializeEx(NULL,COINIT_MULTITHREADED) hence the concurrency model for the main thread is STA and for child thread is MTA. i was able to execute the method from child thread even without initializing the library.
Sukumar
@Sukumar: Marshalling will not kick in if you just pass a raw pointer. How do you pass the object pointer to another thread?
sharptooth
I am creating a COM object as member of the class and passing this class instance to child thread. from child thread thread i will access the com object thorough the instance of the main class
Sukumar
Do you just pass a `Object*` into a C++ function?
sharptooth
This is the class where i will create the objects in main threadBKMpps::BKMpps(const DeviceInfo if(FAILED(hr)) TESTHR(hr); if(m_MppsThread==NULL) m_MppsThread=AfxBeginThread(MPPSProceduresthread,this,THREAD_PRIORITY_LOWEST);//This is the child thread which access the methods of BKMPPS classwhere com object methods are actually invkoed}//}}
Sukumar
>>>>Do you just pass a Object* into a C++ functionI did n't get actually what ur asking
Sukumar
How does the "child" thread get the pointer to the COM object?
sharptooth
The child thread i/p param is instance of the class in which com object has been created. with this instance i will access the methods of that class where actual calls to the com objest made.put in other words i created the object in main thread and using the object from child thread..did u get me?
Sukumar
Hi..plaese let me know if u require extra info abt this
Sukumar
I am not directly passing the COM interface pointer from main thread to child thread. i am passing an instance of the class(not com) in which com object is created through this instance i will access the com object methods..will that fine?
Sukumar
Aha. This means that the other thread just puts its hands onto the same pointer. How on Earth will COM know that it needs to marshall the pointer? It has no idea about that - you're in C++ world and you can pass raw pointers freely between threads.
sharptooth
A: 

Yes, because you are creating an object in one apartment and invoking methods in another one. You have faced no side effects yet. I think because your object is simple (no callbacks, connection points, etc). But it may be changed.

Sergius