Running the thread in the MTA does not require a Windows message mechanism, because COM calls the object's interface through the object's V-Table, so the COM object in the MTA does not synchronize mechanism, and you need to achieve synchronization through the critical regions, events. The interface pointer of the COM object can also be passed directly between the MTA thread.
The component declares the MTA by calling CoinitializeEx (NULL, COINIT_MULTITITED), and the client code calls CoinitializeEx (NULL, COINIT_MULTITHREADED) Create MTA, and only one MTA can be created in a process, so the first call CoinitializeEx is created a MTA, then The MTA is added to the thread of CoinitializeEx again.
There are a total of eight thread mode combinations between clients and components, four of the process:
STA
-
STA
,
STA
-
MTA
,
MTA-STA
,
MTA
-
MTA
There are four in the same process. How to choose a thread mode, the basic rule is to use the components interacting with the user.
STA
Each display window has a message loop,
STA
Natural is very suitable. Components without user interfaces are best used
MTA
,because
MTA
Mode ratio
STA
Faster, because you don't need to synchronize, and
MTA
Until
MTA
Call does not need
Marshal
Interface
STA
Until
STA
or
MTA
Until
STA
Call
Marshal
interface.