Bit About Threading
A single instance of an application is known as the
process. Concurrent execution can be perfumed within a process using threads.
Operating system switches back and forth between threads within a process
using a scheduling algorithm of some sort.
Associated with window. Have message loops that
receive events targetted to the window.
Not associated with Any window. Used for background
processing. It is your responsibility to handle thread synchronization and
prevent deadlocks or racing conditions. Windows guarantee synchronization.
User Interface threads always-own one or more windows.
COM Threading Models
Model (STA + MTA)
A threading model describes the type and degree of
thread safety implemented by a component.
All calls to all instances of the object must always
be on the same thread. There are no
synchronization issues because there is always only one thread no matter how
many object instances there are. The
caller is responsible for ensuring that all calls to all instances are on the
All calls to any given instance of the object must
always be on the same thread, but different instances can be called on
different threads at the same time.
The caller is responsible for ensuring that given an instance, all
calls to that instance happen on the same thread. The object is responsible for synchronizing access to global
data that it owns.
enables all clients to obtain a direct pointer to a component interface,
without going though a proxy and stub service. An apartment is associated
with one or more threads, and one or more COM object instances.
STA has only one thread that crates and call
Most COM objects, ActiveX objects, Object models
commonly used by script
Calls to the object can be on any thread at any
time, including multiple threads at the same time. The object is responsible for all synchronization issues.
Multiple threads can reside in one apartment. There
is only one MTA per process. When additional MTA threads are initialized as
MTA thread types, they live in the same apartment; In addition there is no
need to marshal between threads.
COM object created by MTA thread type must be thread
safe and must provide their own synchronization code. In the MTA model, any
thread can call a COM objects concurrently.
Calls to an object can be on any thread but the
caller guarantees that only one thread is calling into the object at any
Client Initializes the COM libraries by calling the
CoInitialize() API function. Here the client can specify which threading
model to use. A Client regardless of its own threading arrangement can safely
access a COM component built from any threading model. If there is any
incompatibility, the COM run-time library takes step to ensure that the
client and server communicate in a thread-safe manner.