文件与 CreateFile/ReadFile 的内存映射 [英] Memory mapping of files vs CreateFile/ReadFile

查看:31
本文介绍了文件与 CreateFile/ReadFile 的内存映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用内存映射文件读取(常规大小的文件)与使用 CreateFile ReadFile 组合进行相同操作相比有哪些缺点(如果有)?

What are the drawbacks (if any) of using memory mapped file to read (regular sized files) over doing the same using CreateFile ReadFile combination?

推荐答案

有了 ReadFile/WriteFile,你就拥有了确定性的错误处理语义.使用内存映射文件时,通过抛出异常返回错误.

With ReadFile/WriteFile you have deterministic error handling semantics. When you use memory mapped files, errors are returned by throwing an exception.

此外,如果内存映射文件必须访问磁盘(或者更糟糕的是网络),您的内存读取可能需要几秒钟(甚至几分钟)才能完成.根据您的应用,这可能会导致意外停顿.

In addition, if the memory mapped file has to hit the disk (or even worse, the network) your memory read may take several seconds (or even minutes) to complete. Depending on your application, this can cause unexpected stalls.

如果您使用 ReadFile/WriteFile,您可以使用 API 的异步变体来控制此行为.

If you use ReadFile/WriteFile you can use asynchronous variants of the API to allow you to control this behavior.

如果您使用 ReadFile,您还具有更高的确定性性能,特别是如果您的 I/O 模式是可预测的 - 内存映射 I/O 通常是随机的,而 ReadFile 几乎总是串行的(因为 ReadFile 在当前文件位置读取并前进当前文件位置).

You also have more deterministic performance if you use ReadFile, especially if your I/O pattern is predictable - memory mapped I/O is often random while as ReadFile is almost always serial (since ReadFile reads at the current file position and advances the current file position).

这篇关于文件与 CreateFile/ReadFile 的内存映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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