COM uses
the same threading mechanism that Win32 uses. It uses
the Win32 API for creating and synchronizing threads.
However, threading models are referred to differently
in Win32 and COM.
The Win32 Threading Model
Win32
defines two types of threads. User-Interface threads and Worker threads. Each process in Win32 can
have one or more User Interface and/or multiple
Worker threads. User Interface threads have message-loops that
receive events targetted to that window and are hence
associated with one or more windows. Worker threads are used for background
processing and are not associated with any window.
As a
developer, there is one other major difference that
you have to be aware of when developing Win32 threads.
User Interface threads always own one or more windows.
Whenever there is an event for a particular window,
the UI thread takes care of calling the appropriate
method. Since the message loop is executed on the
same UI thread regardless of the thread that sent the
message, synchronization is guaranteed by Windows.
You as the programmer need not write any special code
for thread synchronization. However, if you are
developing worker threads, it is your responsibility
to handle thread synchronization and prevent
deadlocks or racing conditions.
The COM Threading Model
COM
terminology to threads is slightly different. There
are three types of threading models in COM. They are
apartment threading, free threading and rental
threading ( introduced for MTS ) The closest analogy
to Win32 is that UI threads are analogically similar
to the Apartment threading model and worker
threads are analogically similar to the Free threading model.
Apartment Threading Model (single
threaded apartments)
This
model was introduced in the first version of COM with
Windows NT3.51 and later Windows 95. The apartment
model consists of a multithreaded process that
contains only one COM object per thread. Single
Threaded Apartments (STA)- This also means that each
thread can be called an apartment and each apartment
is single threaded. All calls are passed through the
Win32 message processing system. COM ensures that
these calls are synchronized. Each thread has its own
apartment or execution context and at any point in
time, only one thread can access this apartment. Each
thread in an apartment can receive direct calls only
from a thread that belongs to that apartment. The
call parametes have to be marshalled between
apartments. COM handles marshalling between
apartments through the Windows messaging system.
Free Threading Model (multi
threaded apartments)
This
model was introduced with Windows NT 4.0 and Windows95
with DCOM. The free threaded model allows multiple
threads to access a single COM object. Free threaded
COM objects have to ensure thread synchronization and
they must implement message handlers which are thread
aware and thread safe. Calls may not be passed
through the Win32 messaging system nor does COM
synchronize the calls, since the same method may be
called from different processes simultaneously. Free
threaded objects should be able to handle calls to
their methods from other threads at any time and to
handle calls from multiple threads simultaneously.
Parameters are passed directly to any thread since
all free threads reside in the same apartment. These
are also called Multi-Threaded Apartments ( MTA )
Both Apartment and Free
Threaded Model
It is
possible for a process to have both the apartment and
free threaded model. The only restriction is that you
can have only one free threaded apartment but you can
have multiple single threaded apartments. Interface
pointers and data have to be marshalled between
apartments. Calls to objects within the STAs will be
synchronized by Win32 whereas calls to the MTAs will
not be synchronized at all.
Thread Neutral Apartment Model
Components
that use the Thread Neutral Apartment model (TNA),
mark themselves as Free Threaded or Both. Here the
component instances run on the same thread type as
the caller's thread. Each instance of a COM class can
run on a different thread each time a method is
called. When a thread is executing a method in a COM
object, and that method creates a new object, MTS
will suspend the current thread and create a new
thread to handle the new object. Like the MTA, TNAs
allow more than one thread to enter an apartment.
However, once a thread has entered an apartment, it
obtains an apartment-wide lock and no other thread
can enter that apartment until it exits. This model
was introduced into MTS and COM+ to ensure that
context switches are faster.