Re: singel thread v.s. apartment thread
"wtller" <wtller@yahoo.com> wrote in message
news:OyyrzpiXJHA.5828@TK2MSFTNGP03.phx.gbl
you mean in ATL those two just difference in rgs file not in code?
i checke my testing project, and i notice this macro:
_ATL_APARTMENT_THREADED
and also i find this in atlbase.h:
_ATL_SINGLE_THREADED
is this meaning?
Well, since you found the macros, you could have also figured out how
they are used.
ATL has two kinds of reference counts and other similar things that
potentially need to be protected against concurrent modification -
object-level (e.g. the usual object reference count manipulated by
AddRef and Release) and module-level (e.g. count of all currently live
objects, used to implement DllCanUnloadNow). Also, reference counts on
class factories are protected the same way as module-level counts (since
class factories are global singletons).
In legacy single-threaded server, neither kind has to be thread-safe. In
STA server, globals have to be thread-safe (since there could be
multiple threads in multiple STA apartments) but objects don't have to
be (since each one lives entirely on the thread that created it). In MTA
server, both kinds have to be thread-safe.
To implement this, ATL has two classes - CComSingleThreadModel and
CComMultiThreadModel. CComMultiThreadModel defines methods Increment and
Decrement and implements them via InterlockedIncrement et al. It also
defines CriticalSection typedef to a class that actually wraps a
critical section. CComSingleThreadModel is "fake" - its Increment and
Decrement are simply ++ and --, and CriticalSection typedef refers to a
class whose Lock and Unlock methods are no-ops.
Finally, ATL has two typedefs - CComObjectThreadModel is used to protect
object-level counts, and CComGlobalsThreadModel is used for module-level
counts. Macros _ATL_SINGLE_THREADED, _ATL_APARTMENT_THREADED and
_ATL_FREE_THREADED determine which of these two is typedef'ed to
CComSingleThreadModel and which to CComMultiThreadModel.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925