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.
Win32 Threading
Model
User
Interface Thread
Associated with window. Have message loops that
receive events targetted to the window.
Worker
Thread
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
1. Single-threading
Model.
2. Apartment-threading
Model. (STA)
3. Free-threading
Model (MTA)
4. Mixed-threading
Model (STA + MTA)
A threading model describes the type and degree of
thread safety implemented by a component.
Single
threaded
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
same thread.
Apartment
threaded
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.
Apartment-threading model
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
objects.
Most COM objects, ActiveX objects, Object models
commonly used by script
Free
threaded
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.
Rental
threaded
Calls to an object can be on any thread but the
caller guarantees that only one thread is calling into the object at any
time.
COM Client
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.
Reference:
http://blogs.msdn.com/ericlippert/archive/2003/09/18/53041.aspx
http://my.execpc.com/~gopalan/com/com_ravings.html