Transfer "COM thread model"

zhaozj2021-02-16  48

The COM thread model should be difficult in the basic knowledge of COM. These: 1. There is a deep understanding of other basic knowledge of COM (because this topic involves all other basics of COM). 2 The learner knows the thread model of Win32 itself (because the thread model in Windows is based on the Win32 thread model). 3.COM thread model is very abstract, it is not good to understand if you still There is no knowledge point mentioned in 1, 2, you can find some books immediately, quickly add this knowledge, if you have already mastered these knowledge, then give you the imagination, it is easy.

Beginning to tell the term "apartment", the translation is translated as "apartment". This term is abstract. This term is the living space of the COM object. You really want to be an apartment, the thread is living in the apartment. Single-line unit (Single-Threaded Apartment Sta) This room is a single room for rich people living, well-equipped, very good service. Multithreaded Apartment MTA lives in this room, so much People are squeezing in a big room, but they are self-improvement. Sorazen is unable to think. Think: 1 Single-threaded unit and multi-threaded unit? If another person wants to live in a single-line unit Communication, you can't go directly to him, even if you live in a noble single room. But you can call. Remind, the phone can only talk to a person at the same time (they have not used the service of the conference call) .

People living in multi-threaded units have a large window, if people living in single-room (single-line unit) want to communicate with them, come to the window, and this window is bigger than you think, you can let Many people dialogue. People in the same room don't have to say, they can call.

Back to the reality first, the term: Unit If you never need to consider synchronization, you don't need this concept, but COM decides to support powerful multi-threads, and then introduce this concept, and the unit determines his way to communicate with the outside world. A thread dealing with COM objects must first decide which unit to enter.

Single-threaded unit This unit itself can only contain a thread, by calling Coinitialize (0) entry. He has a similar way to the window, recalling the window of the window: Message pumps continue to extract messages from the message queue, then schedule The corresponding window function. The advantage of this is that the window function will never go back, that is, there will never be synchronized. Single-threaded unit also uses the same way of operation (so the main function of the thread in the unit) There must be a message loop): The call to the COM object owned in the unit is queued, only when a call is over, the other call will begin. Then the component object's code is never reinforcing. More Thread unit This unit can contain any number of threads (determined by the operating system). One process can only contain one such cell, all calls CoinitializeEx (null, coinit_multitHeaded) will enter this unit. Whenever The call to the COM object is direct (unscrupulous). Includes threads in this unit with other STA threads. Think: 1 What is the essence of single-threaded units and multi-threaded units? Single thread Unit implementation synchronization, there are many COM libraries intervention, including converting external calls into window messages, and then the window function of that special hidden window converts window messages into function calls for COM objects. Such models can reduce development components Difficulty, but it sacrifices efficiency.

The multi-threaded unit handles all of the synchronization tasks to the component yourself, so the COM objects living in this unit must be strong enough, consider various synchronous problems, not more than a plurality of threads playing when calling the member function of the object.

2 Take a clear relationship between its relationships, the relationship between units, threads, and objects is very important. Do you find out? If you don't get it, the above is also aware of it. The unit is the biggest The unit, he is a thread container. If you call Coinitialize (0), the COM library will create a STA (note that "creation"), your thread will belong to this unit and is the unique member of this unit. If CoinitializeEx NULL, COINIT_MULTITHEADED, and the first requirement to enter the MTA thread, COM library creates a MTA, other rear calls Coinitializeex (null, coinit_multitHeaded) threads can enter (note, "enter") has MTA The thread is a running entity that does not assign resources, but an object in the COM's thread model is closely related to the creation of his thread. The object belongs to a thread, as for this property relationship is in the COM library How to manage, we don't care about him first. In the future, we say that the object created by thread a is the object of thread A. (there is an exception, there is a Single type COM object, this object is really That is, COM is proposing the product before the thread model, which is always attributed to the main STA thread, which is the first thread that calls Coinitialize (0).) If you have a mistake, please refer to.

Example: Www.widgetware.com There is an example of , where the tenth lesson is the example of COM's thread model. I have seen it, there are several, good, you can go see.

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

New Post(0)