Wildcard "com thread model"

Source: Internet
Author: User

I have never been quite clear about the term "apartment", but they always like to answer the question in an attempt to ask people on the Forum: "You should read the" com essence "carefully." In fact, if some of the key parts do not understand it, it won't work even if we read it for ten times. What we need is a suitable teaching material. Today, we occasionally find this article, I hope readers can give their comments. For ease of reading, I made some typographical la s for the original article.

===== Original document ====

General saying "com thread model"

 

Author: Unknown Source: moonlight software station time: moonlight software Station

 

The com thread model should be difficult in the basic knowledge related to com, which may be due to the following reasons:

1. You need to have a deep understanding of other basic knowledge of COM, because this topic involves almost all other basic knowledge of COM.

2. learners must have a good understanding of the Win32 thread model, because the com thread model in Windows is built on the Win32 thread model.

3. The concepts referenced by the COM thread model are very abstract and hard to understand.

If you have not mastered the knowledge points mentioned in 1 or 2, you can immediately find some books and quickly add these knowledge. If you have mastered these knowledge, then give your imagination a bit of oil, easy to use.

 

Start to imagine

Terms

Some apartments are translated as "suites ". This term abstracts the living space of COM objects. You can really imagine an apartment. The thread is the person who lives in the apartment.

Single-threaded apartment sta (single-threaded apartment Sta) is a single room for rich people to live in. It is fully equipped and provides considerate services.

Multithreaded apartment MTA has many people living in such rooms, so many people are crowded in a large room, but they are constantly improving. All of them are very robust.

 

Then think

What are the essential differences between a single-threaded apartment and a multi-threaded apartment?

If another person wants to communicate with a person living in a single-threaded apartment, he cannot contact him directly, Even if you live in a noble single room. But you can call. Remind me that the phone can only talk to one person at a time (they have not used services such as teleconference ). The people who live in a multi-threaded apartment have a big window. If the people who live in a single room (single-threaded apartment) want to communicate with them, just come to the window, in addition, this window is bigger than you think and can be used by many people at the same time. Needless to say, people in the same room can talk directly.

 

Back to reality

Terms

1. If the apartment never needs to consider thread synchronization, this concept is not needed. However, com decided to support strong multithreading, so it introduced this concept, the apartment determines the way the thread communicates with the outside world. Every thread dealing with COM objects must first decide which apartment to enter.

2. Single-threaded apartment. This apartment can only contain one thread and can be entered by calling coinitialize (null. It works in a similar way as a window. Think back to the window running mode: the message pump constantly extracts messages from the message queue and then dispatches them to the corresponding window function. The advantage of doing so is that window functions will never be re-imported, that is, there will never be a synchronization problem. The same operation method is used for a Single-threaded apartment (so the main function of the thread in the apartment must have a message loop ): calls to the COM object owned by the thread in the apartment are queued. Only after one call is completed will the other call start. The code of the component object will never be re-imported.

3. multi-threaded apartment, which can contain any number of threads (the specific number is determined by the operating system ). A process can only contain one such apartment. All calls to coinitializeex (null, coinit_multitheaded) will enter the apartment. The call to the COM object owned by the thread in the apartment is direct (the cross-process is not considered first), including the thread in the apartment and other stathreads.

 

Then think

What are the essential differences between a single-threaded apartment and a multi-threaded apartment?

The single-threaded apartment achieves synchronization, with many com library interventions, including converting external calls into window messages, then the window function of the special hidden window converts the window message to the function call of the COM object. This model reduces the difficulty of developing components, but sacrifices the efficiency. The multi-threaded apartment handed over all the synchronization tasks to the component itself. Therefore, the COM objects in the apartment must be robust enough to consider various synchronization issues, it does not prevent multiple threads from fighting when calling the object's member functions.

 

Clarify their relationships

It is very important to understand the relationship between apartment, thread, and object. Have you figured it out? If you haven't figured it out, you will be confused about the above. The apartment is the largest unit in this area. It is a thread container. If you call coinitialize (0), the com library will create a sta (Note: it is "created"), and your thread will belong to this apartment and be the only member of this apartment. If coinitializeex (null, coinit_multitheaded) is the first thread required to enter the MTA, the com library will create an MTA, and other later calls coinitializeex (null, coinit_multitheaded) the thread will directly enter (note that I am using "enter") an existing MTA. Originally, a thread is a running entity and does not allocate resources. However, in the thread model of COM, an object is closely related to the thread that created it. It is called that the object belongs to a thread, as for how to manage the ownership relationship in the com library, we should not care about it first. Then we will describe the object created by thread A as the object of thread a (with an exception, let's talk about it. There is a single type of COM object, which is the product of COM before proposing the thread model. Such an object always belongs to the main stathread, that is, the first thread that calls coinitialize (0 .)

 

If any, please correct me.

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.