Can my STA object create worker threads?
For some reason, a bunch of COM related stuff's been coming onto my radar lately, so for some reason, I've been dealing with a lot of COM related issues. Go figure this one out.
The other day, I received a message from a co-worker asking me (roughly) "I have an apartment threaded COM component. Does this mean that I can't use threads in the object?"
It's a really good question, and I had to think about it for a while before answering.
CAVEAT: I'm not a COM person, so it's possible the answer is more complicated than this, but here goes.
The simple answer is that by declaring your component as being apartment threading is an indicator to the CREATOR of your object that it's only safe to call the methods on your object from the thread that initially created the object. The contract says NOTHING about how you internally implement your component. So you are free to use whatever internal mechanisms you want in your component.
However, as always, there is a huge caveat.
By declaring your COM object as apartment threaded, you've told COM that the methods on the interfaces to your object can only be called from the thread that created the object. That means that if COM ever drops one of the standard marshallers between your other threads and the interfaces (which shouldn't happen, but might in some COM interop scenarios, or if you're subclassing the standard marshaller, or if your COM object is an inproc handler), then COM's going to enforce the threading model you declared for your object. Which means that COM might start returning RPC_E_WRONGTHREAD to your method calls if they go through the marshaller.
The thing is that you might get away with this for years (since none of the COM marshallers will be involved in most in-proc scenarios), until some point in the future when your component is called in a scenario you don't expect and all of a sudden, the COM marshaller gets in the way and all of a sudden things stop working.
- Anonymous
October 13, 2004
The comment has been removed - Anonymous
October 13, 2004
How anout explain Com to non C programmers? I spent years trying to find out what is was at it's core. I read meaningless articles on monikers (apparantly it's a name) and IUnknown. Then one day I came across one that mentioned VTables in passing. That made it obvious it was a function library, just like a dll. Yet nowhere before or since have I seen it said it's a function library. I don't know what marchalling is (in VB we just compile a class library I think - it's been a few years -- no Iunknown - vb does all that for you). - Anonymous
October 13, 2004
Btw, Mike, thanks for the additional info, it's great.
David, Let me see what I can do. - Anonymous
October 13, 2004
The comment has been removed - Anonymous
October 15, 2004
If you follow COM rules with regard to passing interface pointers between threads, then you should be fine.
However there is another problem with creating worker threads from a DLL (not necessarily an STA COM server - any DLL). It has to do with terminating the threads when the DLL is unloaded.
Clearly, you can't wait for the threads to shutdown in you DllMain(PROCESS_DETACH) since that would deadlock. You could require clients to call a Shutdown() method, or even prevent the DLL from being unloaded by doing LoadLibrary on yourself.
In the COM case it might be tempting to shutdown the threads when your last reference goes away (the moment you start returning TRUE from DllCanUnloadNow). This however is not safe - if the client calls CoUninitialize and this call destroys the COM apartment where your object lives, COM will unload you without even asking DllCanUnloadNow.
A better approach is to give each thread its own reference to the DLL. Before creating the thread, you call LoadLibrary on yourself. When the thread terminates, it calls FreeLibraryAndExitThread. - Anonymous
October 15, 2004
The comment has been removed - Anonymous
October 15, 2004
The comment has been removed - Anonymous
October 16, 2004
The comment has been removed - Anonymous
April 30, 2007
PingBack from http://punkouter.wordpress.com/2007/05/01/on-com/