Coinitialize(nil)和CoInitializeEx(0,COINIT_MULTITHREADED)的区别 [英] Coinitialize(nil) and CoInitializeEx(0, COINIT_MULTITHREADED) difference
问题描述
在线程中,
有差异,如果我使用
in a thread, is there a difference if I use
Coinitialize(nil)
而不是
CoInitializeEx(0, COINIT_MULTITHREADED);
我使用Delphi 7,但我认为其他编程语言可以保留的问题
感谢您的帮助。
I use Delphi 7 but I presume the question can remain for other programming languages Thanks for your help.
推荐答案
前者以将调用线程放入自己的单线程公寓(STA)的方式初始化COM, 。后者通过将调用线程放入共享多线程公寓(MTA)的方式初始化COM。两个公寓有非常不同的语义,特别是在COM对象如何通过线程边界访问。不同公寓的线程必须使用代理来共享COM对象,但COM可以为您提供同步(通过每线程消息队列)。同一个公寓中的线程可以共享COM对象而不使用代理,但必须手动进行同步,例如与关键部分或互斥体。
The former initializes COM in a way that puts the calling thread into its own single-threaded apartment (STA). The latter initializes COM in a way that puts the calling thread into a shared multi-threaded apartment (MTA). The two apartments have very different semantics, especially in how COM objects are accessed across thread boundaries. Threads in different apartments must use proxies to share COM objects, but COM provides synchronization for you (via per-thread messages queues). Threads in the same apartment can share COM objects without using proxies, but must synchronize manually, such as with critical sections or mutexes.
所以是的,有一个区别可以非常显着。请阅读MSDN上的文档,非常详细。
So yes, there is a difference and it can be very significant. Please read the documentation on MSDN, it is very detailed.
这篇关于Coinitialize(nil)和CoInitializeEx(0,COINIT_MULTITHREADED)的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!