In general, the simplest way to view the COM threading architecture is to think of all the COM objects in the process as divided into groups calledApartments. A com Object lives in exactly one apartment, in the sense that its methods can legally be directly called only by a thread that belongs to that apartment. any other thread that wants to call the object must go through a proxy.
There are two types of apartments: Single-threaded apartments, and multithreaded apartments.
- Single-threaded apartments-single-threaded apartments consist of exactly one thread, so all COM objects that live in a single-threaded apartment can receive method callonly from the one thread that belongs to that apartment. all method callto a COM object in a single-threaded apartment are synchronized with the Windows message queue for the single-threaded apartment's thread. A process with a single thread of execution is simply a special case of this model.
- Multithreaded apartments-multithreaded apartments consist of one or more threads, so all COM objects that live in an multithreaded apartment can receive method CILS directly from any of the threads that belong to the multithreaded apartment. threads in A multithreaded apartment use a model calledFree-threading. Callto COM objects in a multithreaded apartment are synchronized by the objects themselves.
A process can have zero or more single-threaded apartments and zero or one multithreaded apartment. The different types of processes can be defined as follows:
- A process that consists of just one single-threaded apartment is referred to asSingle-threaded Process.
- A process that has two or more single-threaded apartments and no multithreaded apartments is calledApartment Model Process.
- A process that has a multithreaded apartment and no single-threaded apartments is referred to asFree-threaded Process.
- A process that has a multithreaded apartment and one or more single-threaded apartments isMixed Model Process.
in reality, all processes are apartment model processes, and some apartments have a single thread and some apartments have multiple threads. the threading model really applies to an apartment, not to a process . it can also apply to a class of objects, but it doesn' t really apply to a component, such as a DLL, but to the object classes within the DLL . different classes in a DLL can have different threading models.
in a process, the main apartment is the first to be initialized. in a single-threaded process, this is the only apartment. call parameters are between aled between apartments, and COM handles the synchronization through messaging. if you designate multiple threads in a process to be free-threaded, all free threads reside in a single apartment, parameters are passed directly to any thread in the apartment, and you must handle all synchronization. in a process with both free-threading and apartment threading, all free threads reside in a single apartment and all other apartments are single-threaded apartments. a process that does com work is a collection of apartments with, at most, one multithreaded apartment but any number of Single-threaded apartments .
The Threading Models in COM provide the mechanic for clients and servers that use different threading ubuntures to work together. callamong objects with different threading models in different processes are naturally supported. from the perspective of the calling object, all callto objects outside a process behave identically, no matter how the object being called is threaded. likewise, from the perspective of the object being called, arriving callbehave identically, regardless of the threading model of the caller.
interaction between a client and an out-of-process object is straightforward, even when they use different threading models because the client and object are in different processes. com, interposed between the client and the server, can provide the code for the threading models to interoperate, using standard authentication aling and RPC. for example, if a single-threaded object is called simultaneously by multiple free-threaded clients, the callwill be synchronized by COM by placing corresponding window messages in the server's message queue. the object's apartment will receive one call each time it retrieves and dispatches messages. however, some care must be taken to ensure that in-process servers interact properly with their clients. (See in-process server threading issues .)
the most important issue in programming with a multithreaded model is to ensure that the Code is thread-safe so that messages intended for a participant thread go only to that thread and access to threads is protected.