The thread mode of COM actually refers to two aspects: one is the thread mode of the client program, and the other is the thread mode supported by the component. There are only two thread modes for the client program.
(STA) and multi-threaded apartment (MTA ). The component supports four thread modes: single (single thread), apartment (STA), free (MTA ),
Both (STA + MTA ).
1. apartment is just a logical concept. One sta can only contain one thread, and one MTA can contain multiple threads. A process can contain multiple Stas, but can only have one MTA.
The threads in the MTA can call the instantiated components in the apartment in parallel without scheduling. Scheduling is required for cross-apartment call component instances. (Unless the free thread scheduler is used)
2. The client program thread determines the type of the client thread when coinitializeex () is called. If you use parameters
If coinit_apartmentthreaded is called, a sta apartment is created, and the customer thread is included in the apartment. If you use parameters
Call coinit_multithreaded to create an MTA apartment and add the thread to the MTA. If an MTA already exists in the process, no new MTA is created,
Only add threads to the existing MTA. Note that each thread must call coinitializeex () to use the COM component.
3. The most important thing about threads is synchronization. Sta solves this problem through window message queue. When the customer thread is called with coinit_apartmentthreaded
Coinitializeex () creates an implicit window with the olemainthreadwndclass window class for this Sta. All pairs created in this apartment
All the calls to the COM Object method will be placed in the message queue of this implicit window. Therefore, every Thread associated with the sta must use getmessage,
Dispatchmessage or similar methods to dispatch window messages. Each thread in the MTA can call instances of the same component object in parallel, which does not guarantee security. Therefore, it is the responsibility to implement synchronous access.
It falls on the component. Note that Stas are synchronized at the apartment level, that is, access to different components in the apartment must be placed in the same message queue, method calls to one instance will affect calls to other instances,
Therefore, the concurrency is low.
4. The interface pointer transmitted between different apartments must be scheduled. This is mainly for Synchronous calls to components. It is implemented through coexternalinterthreadinterfaceinstream and cogetinterfaceandreleasestream. Very simple.
5. Single components are special. They can only be executed in a single thread. First, you must note that the first
Coinit_apartmentthreaded the thread that calls coinitializeex () is called the master Sta. Use each time
The Single Component created by cocreateinstance () is actually created in the master STA, no matter who calls
Cocreateinstance. All calls to this single component method must pass through this master Sta.
6. If the sta component is created, it is directly created and called directly. If the sta creates an MTA component, the system creates an MTA for the component, and the sta accesses the component through proxy. If
Stas creates a both component, which is directly created and called directly. If the MTA creates an sta component, the system creates an sta for the component and the MTA accesses the component through a proxy. If the MTA type is created
A component is directly created and called directly. If MTA creates a both component, it is directly created and called directly. It can be seen that if both the client program and component support the same thread mode, com will allow the client program
Directly call the object in sequence, which will produce the best performance.
7. The both component is ready. You can directly create and call it either by using sta or MTA. However, cross-apartment calls still need to go through agents. To further obtain the best performance, you can
To use the free thread scheduler (FTM ). Note that FTM can be used for other types of components, but it is best to use FTM by both. FTM implements the imarshal interface,
When the two scheduling interface pointer functions are scheduled, these two functions (see figure 5) Call the relevant functions in imarshal internally, it determines that an interface is directly returned if the scheduling occurs between apartments in a process.
Pointer; if scheduling occurs between processes or between remote computers, the standard scheduler is called and a pointer to the proxy object is returned. Therefore, FTM is used, and no scheduling interface pointer is required between apartments.
Now !!
8. Although FTM is good, FTM components must comply with certain restrictions: FTM objects cannot directly have interface pointers for objects that do not implement FTM; objects Using FTM cannot be referenced by other apartment objects.
9. Global interface table (GIT ). The role scope is in the process. You can store the interface pointer in the table and retrieve it from other apartments. Git automatically schedules the apartment rooms, so it is very convenient.
Git is accessed through iglobalinterfacetable. Create a clsid_stdglobalinterfacetable pair
You can call it.