Thread mode in COM components

zhaozj2021-02-11  236

Threading mode in COM components Lostall · COM concentration

Referring to COM's thread mode, it is actually two aspects, one is the thread mode of the client program, one is the thread mode supported by the component. The client's thread mode is only two, single-threaded apartment (STA) and multi-threaded apartment (MTA). The thread mode supported by the components has four: Single (single thread), Apartment (STA), Free (MTA), Both (STA MTA).

1. The apartment is just a logical concept. A STA can only contain a thread, and a MTA can contain multiple threads. A process can contain multiple STAs, but there can be only one MTA. The threads in the MTA can call the components of this apartment in parallel, without the need to schedule. Inter-apartment calling component instance must be scheduled. (Unless free thread scheduler)

2, the client's thread is to determine the type of the client thread when calling CoinitializeEx (). If you call with parameter coinit_apartmentthreadeded, a STA apartment will be created, and the customer thread is included in this apartment. If you are called by parameter coinit_multithreadeded, create a MTA apartment to add threads to this MTA; if there is an MTA in the process, you don't create a new MTA, just add threads to existing MTA. Note that each thread must call CoinitializeEx () to use COM components.

3. The most important thing for thread is synchronous problem. STA is to solve this problem through a window message queue. When the customer thread calls CoinitializeEx () with COINIT_APARTMENTTHREADED (), an implicit window with the OLEMAINTHREADWNDCLASS window class will be created. All calls to the COM object method established in this apartment will be placed in the message queue of this hidden window. So each of the threads associated with the STA must assign window messages with getMessage, DispatchMessage, or the like. Each of the MTA can call an instance of the same component object in parallel, so that security is not guaranteed, so the responsibility to realize synchronous access is on the component. Note that STA synchronization is an apartment-level, that is, the access to different components in the apartment must be placed in the same message queue, and the method of call to an instance affects the call to other instances, so the degree of concurrency is very low.

4. Delivery interface pointers in different apartments must be scheduled. This is mainly to synchronize the call to the component. Implemented with ComarshalinterthreadInterfaceInstream and CogetherInterfaceAndreleaseStream. Very simple.

5, SINGLE type assembly is very special, it can only be executed in a single thread. The first thing to explain is the first process called CoinitializeEx () in a process called a primary STA. Every time you use CocreateInstance (), the Single component created is actually created in this primary STA, regardless of who calls the CocreateInstance () this function. All calls to this Single component method must pass this master STA.

6. If the STA creates a STA component, it is created directly and is directly called. If the STA creates a MTA component, the system creates a MTA for the component, and the STA accesses the component through the proxy. If STA creates a Both type component, it is created directly and is directly called. If the MTA creates a STA component, the system creates a STA for the component, and the MTA accesses the component through the proxy. If MTA creates a MTA component, it is created directly and is directly called. If MTA creates a Both component, it is created directly and is directly called. It can be seen that if the client and components support the same thread mode, COM allows the client to directly call the object, which will produce optimal performance.

7, Both type components are already very good, whether it is STA or MTA, you can create calls directly. But inter-apartment calls still have to pass by agents. In order to achieve the best performance, a free thread scheduler (FTM) can be used. Note that other types of components can also use FTM, just by BOTH, FTM, available is best effect. FTM implements interface iMarshal, when the two scheduling interface pointer functions, these two functions (see 5) call the IMARSHAL in the iMarshal, and determine that if the schedule occurs, it will return directly between the scheduled apartments. Interface pointer; if the schedule occurs between the process or between the remote computer, the standard scheduler is called and returns a pointer to the proxy object. So it can be seen using FTM, even if there is no scheduling interface pointer between the apartments! ! 8, although ftm is good, but the components using FTM must follow certain restrictions: Objects using FTM cannot directly have interface pointers that do not implement FTM objects; objects with FTM cannot have references to other apartment object agents.

9. Global Interface Table (Git). The scope is within the process. You can store the interface pointer to the table, then remove it in other apartments, Git automatically performs scheduling between apartments, so it is very convenient. Git is accessed through IGLOBALINTERFACETABLE. You can call it by creating a CLSID_STDGLOBALINTERFACETABLE.

转载请注明原文地址:https://www.9cbs.com/read-4504.html

New Post(0)