COM
WINDOWS originally opened
Never support threads, so com
I didn't support threads, followed by threads in Windows.
Popularization in the system, COM
It also provides support for threads. Now com
Two thread models are supported, a graphical user interface application for user-driven, a worker component for not displaying a user interface.
In order to prevent non-threaded safety code, COM also provides thread security support, and the basic element is "apartment". Two types of apartments are defined in COM: SLR Apartment (STA) and Multi-Threaded Apartment (MTA). This article mainly introduces STA.
As the name suggests, each single-threaded apartment is only one thread apartment, and each process can contain any single-threaded apartment. Any call to the object in a single-threaded apartment will be automatically synchronized and distributed using the Windows Message Team, so any support for the STA mode must contain a message loop. Although the STA mode relies on the Windows message queue, this does not mean that the STA mode needs to display the user interface. COM will automatically create a hidden window for each STA. This structure solves multiple clients simultaneously call a STA. Synchronization problem in the ground object.
The component declares supports the STA mode by calling CoinitializeEx (NULL, COINIT_APARTMENTTHREADED). STA is instantiated all objects in its thread. All calls to these objects will be executed in the creation of them, so in components Calling getCurrentThreadID and other APIs may not be what we want. Similarly, any thread at the client must create STA by calling Coinitializeex, and any found object in this thread will become a member of its STA, and the object pointer cannot be used directly in other apartments.
The client and components in the DCOM can run on a different computer in the network. First, the client thread calls the remote component in the local proxy, proxy will call the parameter serialization call IRPCChannelBuffer :: SNEDRece to create a new thread to transfer the package data to the server through the RPC, and the client waits for returning information in the message loop, the server receives the data After sending a message to the hidden window queue of the component object, the STUB of this component receives the message and unpacking the parameter, then call the actual component object to complete the request. The process of parameter packaging in this working mechanism becomes Marshaling.
The COM also provides a Marshal mechanism for an object interface pointer in another apartment in an apartment. ComarshalintertHreadInterfaceInstream converts an interface pointer to a stream object, the flow object can be passed to another apartment through global variables and call CoGetInterfaceAndReleaseStream to convert the stream object to the component object interface pointer.