本文介绍了我需要编组IStream返回的CreateStreamOnHGlobal,用于线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个COM流对象(),使用。



以在同一进程内的不同线程中使用它。我需要编组流对象本身( CoMarshalInterface 等)?或者已经是线程安全了吗?



已编辑,读取/写入/

解决方案

COM将 IStream 视为一种特殊类型的接口,线程。这是必要的,以便其他接口可以在 IStream 中跨线程边界进行封送,使用。



信息可以在Dobb博士的2003年文章中找到:。



更新:



最初发布的答案不完全正确。 OLE提供的 IStream 接口的实现,由,并通过可以跨同一进程的线程安全访问。 >

文档分散,很难找到。 CoMarshalInterThreadInterfaceInStream 说明以下内容:

类似的信息可用于 CreateStreamOnHGlobal 来自:

这些保证通常不适用于所有 IStream 实现。如果你想安全地玩,你可以总是使用 CoMarshalInterThreadInterfaceInStream 跨线程边界封送接口,即使不是绝对必要。这是。请记住,这是 marshal once - unmarshal once 。如果要从多个线程中解封接口,可以将接口放入。


I have a COM stream object (IStream), created with CreateStreamOnHGlobal.

I want to use it across different threads within the same process. Do I need to marshal the stream object itself (with CoMarshalInterface etc)? Or is it thread-safe already?

EDITED, reads/writes/seeks are properly synchronized with locks in my codes.

解决方案

COM treats IStream as a special type of interface, which can be safely used across threads. This is necessary so that other interfaces can be marshaled across thread boundaries in an IStream using CoMarshalInterThreadInterfaceInStream.

Additional information can be found in a 2003 article of Dr. Dobb's: Marshaling COM interfaces.

Update:

The answer, as initially posted, is not entirely correct. The OLE-provided implementation of the IStream interface, as returned by CreateStreamOnHGlobal and indirectly created through CoMarshalInterThreadInterfaceInStream can be safely accessed across threads in the same process.

Documentation is scattered and hard to come by. CoMarshalInterThreadInterfaceInStream states the following:

Similar information is available for CreateStreamOnHGlobal from SHCreateMemStream:

The guarantees do not generally hold true for all IStream implementations. If you want to play it safe, you can always marshal interfaces across thread boundaries using CoMarshalInterThreadInterfaceInStream, even if not strictly necessary. It is never harmful to marshal an interface pointer in this way because COM is smart enough not to marshal (or remarshal) the pointer if marshaling isn't necessary. Keep in mind though that this is marshal once - unmarshal once. If you want to unmarshal the interface from multiple threads you can place the interface into the Global Interface Table.

这篇关于我需要编组IStream返回的CreateStreamOnHGlobal,用于线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 00:49