We'll look at the apartment thread first. One can think of an apartment as being an isolated box, which contains at most a single thread. The apartment is composed of that single thread, plus a Windows message queue, and some OS support to ensure that everything else deals with the apartment thread through the message queue, and never directly. Because all interaction is funneled through the message queue, all operations are serialized, and thus there is no need for synchronization within an apartment thread. COM objects created within an apartment thread 'live' in that apartment - it's created there, does work during it's lifetime there, then is destroyed there. If any other thread wants to interact with that object, an interface pointer will have to be marshalled to the other thread -- either by Windows, or you. Thus, COM objects within an apartment thread are 'local' to that thread.
Next, we'll examine free threads. Between free threads, there's
no synchronization other than what you provide. Also, COM objects (and
interfaces) can be shared between free threads without having to marshal
them, thus COM objects created by a free thread are in no way 'local'
to that thread. Note that although some books refer to there
being a 'free thread apartment', so far as I can tell, that's pretty much
a meaningless statement: think 'free thread' instead. It is instructive
to consider when marshalling/synchronization does and doesn't occur:
From | To | Synchronization provided by Windows? | Marshalling Interfaces neccessary? |
Apartment | Apartment | Yes | Yes |
Free | Free | None whatsoever | No |
Apartment | Free | None whatsoever | Out-of-proc:yes, otherwise no |
Free | Apartment | Yes | Yes |
All Rights Reserved 1997 by Michael William Panitz (mpanitz@cascadia.ctc.edu)
Home Page
Please note that OLE, COM, ActiveX, book titles, etc are NOT in any
way reserved or trademarked by Mike, but instead belong to their resepective
owners. The author would also like to gratefully acknowledge the MS Visual
C++ help files as being a much appreciated source of information, particularly
for function/interface prototypes, data type definitions, etc.