我找不到任何有关 MTA 兼容 COM 的先决条件的源代码。我尝试将对象的 ThreadingModel 注册表项从 Apartment 更改为 Both,当辅助线程在访问任何数据之前调用该方法时,会导致崩溃。
如果 STA COM 需要消息泵,那么 MTA COM 对象需要什么样的管道代码?
最佳答案
我不认为 MTA 有什么特别之处,只是您需要使用互斥体等同步原语来同步对内部结构的访问。 "Multithreaded Apartments" 是否没有给你你需要的一切?
引用文档,重点是我的:
Because calls to objects are not serialized in any way, multithreaded object concurrency offers the highest performance and takes the best advantage of multiprocessor hardware for cross-thread, cross-process, and cross-machine calling. This means, however, that the code for objects must provide synchronization in their interface implementations, typically through the use of synchronization primitives such as event objects, critical sections, mutexes, or semaphores, which are described later in this section. In addition, because the object doesn't control the lifetime of the threads that are accessing it, no thread-specific state may be stored in the object (in thread local storage).
关于winapi - MTA COM 服务器的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22912271/