Apartments: The Classic COM Solution
The solution
used by classic COM is deceptively simple:
each component declares its synchronization needs. Classic COM makes
sure that instances (objects) of that class always reside in an
execution context that fits their declared requirements, hence the
term apartment. A component declares its
synchronization needs by assigning a value to its
ThreadingModel named-value in the Registry. The
value of ThreadingModel determines the
component’s threading model. The available values under classic
COM are Apartment, Free,
Both or no value at all.
Components that set their threading model to
Apartment or leave it blank indicate to COM that
they cannot handle concurrent access. COM places these objects in a
single-threaded environment called a single-threaded
apartment
(STA). STA
objects always execute on the same STA thread, and therefore do not
have to worry about concurrent access from multiple threads.
Components that are capable of handling concurrent access from
multiple clients on multiple threads set their threading model to
Free. COM places such objects in a
multithreaded
apartment
(MTA).
Components that would like to always be in the same apartment as
their client set their threading model to Both.
Note that a Both component must be capable of
handling concurrent access from multiple clients on multiple threads
because its client may be in the MTA.
As discussed in Chapter 2, classic COM marshals away the thread differences between the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access