＜本文发表于: 相约加拿大:枫下论坛 www.rolia.net/f ＞
DCOM Threading Models, Server, Client, and Object Instances:
After a few reading and testing, this is clear.
For the server:
Single threaded model will only create one thread for DCOM object,
More than one instances can be created from one object (class). Then
each object is a different internal pointer, and is associated with
one client and one possible callback connnection point. There is only
one hidden window serving as message pump for DCOM:
(OleMainThreadWndName (with class OleMainThreadWndClass))
All the incoming calls to the server are linedup and thread safe, no
matter the client is free-model or not.
If Singelton class factory is used, only one instance of the object
is created, all clients share the same server instance.
The callback should be in the same thread, otherwise thread marshaling
may be needed to create a new pointer for the callback. Create another
hidden window in the main thread will garantee
Multithreaded run time library can be linked for the server if the server
involves different threads even for single threaded server. Only the service
is single threaded, internally there can be several working threads to
support the service.
Apartment threaded model can generate thread pool if inheritted from
CComAutoThreadModule rather than CComModule. Inside each thread, there
is a call pair to CoInitialize and CoUninitialize to create an
apartment for the thread. Still there is only one hidden window for
message pump, and all calls are lined-up no matter the thread model of
the client. But each object instance is associated with a thread.
Free threaded model will generate a thread for each instance, but
there is no message pump window, all access should be thread safe
designed by the programmer.
For the client:
Advantage of using free-threaded client, no need to call CoInitialize
and CoUninitialize in each thread and marshaling the object pointer for
each thread. All calls can be in the same main free-thread apartment, which
is created in the main thread by CoInitializeEx. Plus if the server supports
free-threaded model, it will be all multi-threaded access for all the calls.
If the server is single threaded apartment, it does not hurt anything,
the server will line up all incoming calls. So in this case, the DCOM will
block the program flow even the client is multi-threaded.
Apartment threaded client, each apartment is within a thread, and is created
by calling CoInitialize and destroyed with CoUninitialize call. This will
generate a message pump, and add the apartment into the existing message pump.＜本文发表于: 相约加拿大:枫下论坛 www.rolia.net/f ＞