Coinitialize(nil)和CoInitializeEx(0,COINIT_MULTITHREADED)的区别 [英] Coinitialize(nil) and CoInitializeEx(0, COINIT_MULTITHREADED) difference

查看:278
本文介绍了Coinitialize(nil)和CoInitializeEx(0,COINIT_MULTITHREADED)的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在线程中,
有差异,如果我使用

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功能

CoInitializeEx函数

进程,线程和公寓

这篇关于Coinitialize(nil)和CoInitializeEx(0,COINIT_MULTITHREADED)的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆