C++ boost.asio 和文件 i/o 有什么关系?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/378515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the deal with boost.asio and file i/o?
提问by Doug T.
I've noticed that boost.asio has a lot of examples involving sockets, serial ports, and all sorts of non-file examples. Google hasn't really turned up a lot for me that mentions if asio is a good or valid approach for doing asynchronous file i/o.
我注意到 boost.asio 有很多涉及套接字、串行端口和各种非文件示例的示例。对于我来说,Google 并没有真正提到 asio 是否是执行异步文件 i/o 的好方法或有效方法。
I've got gobs of data i'd like to write to disk asynchronously. This can be done with native overlapped io in Windows (my platform), but I'd prefer to have a platform independent solution.
我有大量数据想异步写入磁盘。这可以通过 Windows(我的平台)中的原生重叠 io 来完成,但我更喜欢有一个独立于平台的解决方案。
I'm curious if
我很好奇
- boost.asio has any kind of file support
- boost.asio file support is mature enough for everyday file i/o
- Will file support ever be added? Whats the outlook for this?
- boost.asio 有任何类型的文件支持
- boost.asio 文件支持对于日常文件 i/o 已经足够成熟
- 会添加文件支持吗?对此有何展望?
采纳答案by vividos
Has boost.asio any kind of file support?
boost.asio 有任何类型的文件支持吗?
Starting with (I think) Boost 1.36 (which contains Asio 1.2.0) you can use [boost::asio::]windows::stream_handle or windows::random_access_handle to wrap a HANDLE and perform asynchronous read and write methods on it that use the OVERLAPPED structure internally.
从(我认为)Boost 1.36(包含 Asio 1.2.0)开始,您可以使用 [boost::asio::]windows::stream_handle 或 windows::random_access_handle 来包装 HANDLE 并对其执行异步读写方法在内部使用 OVERLAPPED 结构。
User Lazin also mentions boost::asio::windows::random_access_handle that can be used for async operations (e.g. named pipes, but also files).
用户 Lazin 还提到 boost::asio::windows::random_access_handle 可用于异步操作(例如命名管道,也包括文件)。
Is boost.asio file support mature enough for everyday file i/o?
boost.asio 文件支持对于日常文件 i/o 是否足够成熟?
As Boost.Asio in itself is widely used by now, and the implementation uses overlapped IO internally, I would say yes.
由于 Boost.Asio 本身现在被广泛使用,并且实现内部使用重叠 IO,我会说是的。
Will file support ever be added? Whats the outlook for this?
会添加文件支持吗?对此有何展望?
As there's no roadmap found on the Asiowebsite, I would say that there will be no new additions to Boost.Asio for this feature. Although there's always the chance of contributors adding code and classes to Boost.Asio. Maybe you can even contribute the missing parts yourself! :-)
由于在Asio网站上没有找到路线图,我想说 Boost.Asio 不会为此功能添加新功能。尽管贡献者总是有机会向 Boost.Asio 添加代码和类。也许您甚至可以自己贡献缺失的部分!:-)
回答by moof2k
boost::asio file i/o on Linux
Linux 上的 boost::asio 文件输入/输出
On Linux, asio uses the epoll
mechanism to detect if a socket/file descriptor is ready for reading/writing. If you attempt to use vanilla asio on a regular file on Linux you'll get an "operation not permitted" exception because epoll does not support regular files on Linux.
在 Linux 上,asio 使用该epoll
机制来检测套接字/文件描述符是否已准备好进行读/写。如果您尝试在 Linux 上的常规文件上使用 vanilla asio,您将收到“不允许操作”的异常,因为epoll 不支持 Linux 上的常规文件。
The workaround is to configure asio to use the select
mechanism on Linux. You can do this by defining BOOST_ASIO_DISABLE_EPOLL
. The trade-off here being select tends to be slower than epollif you're working with a large number of open sockets. Open a file regularly using open()
and then pass the file descriptor to a boost::asio::posix::stream_descriptor
.
解决方法是将 asio 配置为select
在 Linux 上使用该机制。您可以通过定义来做到这一点BOOST_ASIO_DISABLE_EPOLL
。如果您正在使用大量打开的套接字,那么这里的权衡选择往往比 epoll 慢。定期使用打开文件open()
,然后将文件描述符传递给boost::asio::posix::stream_descriptor
.
boost::asio file i/o on Windows
Windows 上的 boost::asio 文件输入/输出
On Windows you can use boost::asio::windows::object_handle
to wrap a Handle
that was created from a file operation. See example.
在 Windows 上,您可以boost::asio::windows::object_handle
用来包装Handle
从文件操作创建的 。参见示例。
回答by Evgeny Lazin
boost::asio::windows::random_access_handle is the easiest way to do this, if you need something advanced, for example asynchronous LockFileEx or something else, you might extend asio, add your own asynchronous events. example
boost::asio::windows::random_access_handle 是最简单的方法,如果你需要一些高级的东西,例如异步 LockFileEx 或其他东西,你可以扩展 asio,添加你自己的异步事件。例子
回答by unixman83
ASIO supports overlapped I/O on Windows where support is good. On Unixes this idea has stagnated due to:
ASIO 在支持良好的 Windows 上支持重叠 I/O。在 Unix 上,由于以下原因,这个想法停滞不前:
- Files are often located on the same physical device, accessing them sequentially is preferable.
- File requests often complete very rapidly because they are physically closeby.
- Files are often critical to complete the basic operation of a program (e.g. reading in its configuration file must be done before initializing further)
- 文件通常位于同一物理设备上,最好按顺序访问它们。
- 文件请求通常很快完成,因为它们在物理上很近。
- 文件通常对于完成程序的基本操作至关重要(例如,必须在进一步初始化之前读取其配置文件)
The one common exception is serving files directly to sockets. This is such a common special-case that Linux has a kernel functionthat handles this for you. Again, negating the reason to use asynchronous file I/O.
一个常见的例外是直接向套接字提供文件。这是一个非常常见的特殊情况,Linux 有一个内核函数可以为您处理这个问题。再次否定使用异步文件 I/O 的原因。
In Short: ASIO appears to reflect the underlying OS design philosophy, overlapped I/O being ignored by most Unix developers, so it is not supported on that platform.
简而言之:ASIO 似乎反映了底层的 OS 设计理念,大多数 Unix 开发人员都忽略了重叠 I/O,因此该平台不支持它。
回答by Zack Yezek
Linux has an asio Library that is no harder to use than Windows APIs for this job (I've used it). Both sets of operating systems implement the same conceptual architecture. They differ in details that are relevant to writing a good library, but not to the point that you cannot have a common interface for both OS platforms (I've used one).
Linux 有一个 asio 库,对于这项工作,它并不比 Windows API 更难使用(我已经使用过它)。两组操作系统都实现了相同的概念架构。它们在与编写好的库相关的细节上有所不同,但不至于不能为两个 OS 平台拥有一个通用接口(我使用过一个)。
Basically, all flavors of Async File I/O follow the "Fry Cook" architecture. Here's what I mean in the context of a Read op: I (processing thread) go up to a fast food counter (OS) and ask for a cheeseburger (some data). It gives me a copy of my order ticket (some data structure) and issues a ticket in the back to the cook (the Kernel & file system) to cook my burger. I then go sit down or read my phone (do other work). Later, somebody announces that my burger is ready (a signal to the processing thread) and I collect my food (the read buffer).
基本上,所有风格的异步文件 I/O 都遵循“Fry Cook”架构。这就是我在阅读操作上下文中的意思:我(处理线程)走到快餐柜台 (OS) 并要一个芝士汉堡(一些数据)。它给了我一份我的订单(一些数据结构)的副本,并在后面向厨师(内核和文件系统)发出一张票来烹饪我的汉堡。然后我去坐下或阅读我的手机(做其他工作)。后来,有人宣布我的汉堡准备好了(给处理线程的一个信号),我收集了我的食物(读取缓冲区)。