Thread mode (excellent) in COM components)

Source: Internet
Author: User
Thread mode in COM Component

1.

Submit
And the thread mode of COM actually refer to two aspects: one is the thread mode of the customer program, and the other is the thread mode supported by the component. There are only two thread modes for the client program, single-threaded apartment (STA)
And multi-threaded apartment (MTA ). The component supports four thread modes: single (single thread), apartment (STA), free (MTA ),
Both (STA + MTA ).

2.

Public
It is only 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 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 ).

3.

Customer
The thread of the user program determines the type of the customer thread when calling coinitializeex. 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.

4.

Line
The most important thing is synchronization. Sta solves this problem through window message queue. When the customer thread
When coinit_apartmentthreaded calls coinitializeex (),
The hidden window of the olemainthreadwndclass window class. All calls to the COM Object method established in this apartment will be placed in the message queue of this implicit window. So
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, so as not to ensure security.
The responsibility for implementing synchronous access falls on the component. Note that Stas synchronization is at the apartment level, that is, access to different components in the apartment must be placed in the same message queue.
Affects calls to other instances, so the concurrency is low.

5.

The interface pointer passed between different apartments must be scheduled. This is mainly for Synchronous calls to components. It is implemented through coexternalinterthreadinterfaceinstream and cogetinterfaceandreleasestream. Very simple.

6.

Single
It can only be executed in a single thread. First, it must be noted that the first thread in a process is called with coinit_apartmentthreaded
The coinitializeex () thread is called the master Sta. Each time the Single Component created with cocreateinstance () is actually created in this
In the master STA, no matter who calls the cocreateinstance () function. All calls to this single component method must pass through this master Sta.

7.

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 the sta creates a both component, it 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 MTA creates an MTA component, it 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 to directly call the object, which will produce the best performance.

8.

Both
You can directly create and call a Stas or MTA component. However, cross-apartment calls still need to go through agents. To get the best performance, you can 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 interfaces are scheduled
When the pointer function is used, the two functions (see figure 5) Call the relevant functions in imarshal internally and determine that if the scheduling occurs between apartments in a process, the interface pointer is directly returned;
If scheduling occurs between processes or between remote computers, the standard scheduler is called and a pointer to the proxy object is returned. So we can see that FTM is used, and no scheduling interface pointer is required between apartments !!

9.

Although FTM is good, FTM components must comply with certain restrictions: (for details, see the book)
Objects Using FTM cannot directly have interface pointers for objects without FTM implementation;
Objects Using FTM cannot be referenced by other apartment objects.

10.

All
Bureau 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 uses
Iglobalinterfacetable. You can call it by creating an object whose CLSID is clsid_stdglobalinterfacetable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.