×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

你这么说我越发哆嗦了. 从前做过一个项目有类似的问题, 附上当时的技术纪录, 只供参考, 再多咱也不会了. 另外static可以用.

本文发表在 rolia.net 枫下论坛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.更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 初学COM,请高手指教,也欢迎初学者讨论。题目是:“如何在多个COM OBJECT之间共享数据?”
    • 用Singleton and single-threaded model.
      • 果然是高手,两句话我就~!·#¥%。:-)。Singleton是否指Singleton的COM?COM中好象不能用STATIC VARIABLE吧?如果这样,怎样保持数据?能否解释一下SINGLE-THREADED MODEL?谢谢!
        • 你这么说我越发哆嗦了. 从前做过一个项目有类似的问题, 附上当时的技术纪录, 只供参考, 再多咱也不会了. 另外static可以用.
          本文发表在 rolia.net 枫下论坛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.更多精彩文章及讨论,请光临枫下论坛 rolia.net
          • 谢谢指教。太深了,留此存档,以后慢慢体会。STATIC member VARIABLE 在COM中可以用吗?为什么在VC中用了STATIC MEMBER VARIABLE 后出现“error LNK2001: unresolved external symbol "private: static long CJobID::m_nID" (?m_nID@CJobID@@0JA)”?
            • 没有用过static member variable in class, 在class外用static定义好象可以. 太复杂的东东我也搞不懂, 好象是VC compiler会产生个内部variable name or function name for class members.
            • You only declared the static member, you need to define it (initialize ) somewhere in the .cpp file. strictly speaking, C++ forbids initializing static member variable in the class declaration(.h file), unless it is a constant integer type.
              • 有道理。不好意思,看来还是基本功不过关。初了参数传递外,有没有简便的机制实现COM内外的数据传递?