More and more COM objects are written to be 'Both' threaded because it enables
creating the object in either a COM STA or MTA. If the thread that creates the 'Both'
threaded COM object is initialized to be in an STA by using
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED) or
the object will be created in the same STA. If the thread is instead initialized to be in the
MTA by using
CoInitializeEx(NULL, COINIT_MULTITHREADED), the object will be
created in the process's one and only MTA.
The developer of the object is thus forced to code for simultaneous access by multiple threads,
typically by using a Critical Section. Note that any global and static variables need to be
protected anyway. What I am presenting here optimizes the code that protects the per object
instance state. I also want to point out that if the object in question aggregates the Free
Threaded Marshaller (FTM), this technique should not be used.
CComSmartAutoCriticalSection is an extremely simple class that wraps
CRITICAL_SECTION. The definition of this class is as follows:
m_bInMTA = FALSE;
m_bInMTA = TRUE;
The constructor detects whether the creating thread belongs to a COM STA or MTA and
initializes the private member variable m_bInMTA. The
call will return S_FALSE if the calling thread belongs to an STA or
if the calling thread belongs to an MTA. If the object is being created in the MTA, the
m_csSafeAccess critical section is initialized..
The destructor deletes the m_csSafeAccess critical section if the object is created in
Unlock() methods invoke the WIN32
respectively ONLY IF the object is created within the MTA.
Within the C++ class the implements your COM object, define a member variable of type
class ATL_NO_VTABLE CSomeCOMClass : <inheritance list>
The implementation functions for the interface properties and methods should be wrapped
HRESULT hResult = S_OK;