如何亲热跨平台的异步文件I / O在C ++ [英] How to perfrom Cross-Platform Asynchronous File I/O in C++

查看:267
本文介绍了如何亲热跨平台的异步文件I / O在C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个应用程序需要使用大量的音频多的样本,通常是在面积约为50 MB。一个文件包含大约80个人短录音,可以通过我的应用程序在任何时候获得回放。出于这个原因,所有的音频数据被加载到内存中以便快速访问。

I am writing an application needs to use large audio multi-samples, usually around 50 mb in size. One file contains approximately 80 individual short sound recordings, which can get played back by my application at any time. For this reason all the audio data gets loaded into memory for quick access.

不过,加载这些文件中的一个时,可以采取许多秒投入到内存,因为我需要阅读大量与ifstream的数据,这意味着我的程序GUI被暂时冻结。我曾尝试内存映射我的文件,但这造成巨大的CPU峰值和音频每次一团糟,我需要跳转到不同区域中的文件,这是不能接受的。

However, when loading one of these files, it can take many seconds to put into memory because I need to read a large amount of data with ifstream, meaning my program GUI is temporarily frozen. I have tried memory mapping my file but this causes huge CPU spikes and a mess of audio every time I need to jump to a different area of the file, which is not acceptable.

因此​​,这使我认为,在执行异步文件的读取能解决我的问题,那就是得到的数据在不同的进程读取并在完成调用一个函数。这需要在Mac OS X和Windows,并在C ++中同时兼容。任何人都可以给我一些帮助吗?

So this has led me to think that performing an Asynchronous file read will solve my problem, that is the data gets read in a different process and calls a function on completion. This needs to be both compatible for Mac OS X and Windows and in C++. Can anyone give me some help?

在此先感谢: - )

编辑:不想使用Boost库,因为我想保持小code基

Don't want to use the Boost library because I want to keep a small code base.

推荐答案

升压有一个ASIO库,我没有使用过(它不认可的第三方库的美国航空航天局的名单上)。

boost has an asio library, which I've not used before (it's not on NASA's list of approved third-party libraries).

我自己的方法已写入文件阅读code两次,一次用于Windows,曾经为POSIX AIO API,然后只需选择正确的与链接。

My own approach has been to write the file reading code twice, once for Windows, once for the POSIX aio API, and then just pick the right one to link with.

对于Windows,使用重叠的(你必须启用它在调用的CreateFile,然后通过一个OVERLAPPED结构,当你读)。你可以把它设置在完成(ReadFile的)事件或调用完成回调(ReadFileEx)。你可能需要改变你的主事件循环使用MsgWaitForMultipleObjectsEx所以你可以等待I / O事件或允许回调运行,除了接受WM_窗口消息。 MSDN有这些功能的文档。

For Windows, use OVERLAPPED (you have to enable it in the CreateFile call, then pass an OVERLAPPED structure when you read). You can either have it set an event on completion (ReadFile) or call a completion callback (ReadFileEx). You'll probably need to change your main event loop to use MsgWaitForMultipleObjectsEx so you can either wait for the I/O events or allow callbacks to run, in addition to receiving WM_ window messages. MSDN has documentation for these functions.

有关Linux中,要么fadvise和epoll的,它会使用预读缓存,或则aio_read这将使实际异步读取请求。在请求完成时,你应该用它来发布一个XWindows的消息,你会得到一个信号,并唤醒你的事件处理循环。

For Linux, there's either fadvise and epoll, which will use the readahead cache, or aio_read which will allow actual async read requests. You'll get a signal when the request completes, which you should use to post an XWindows message and wake up your event processing loop.

两者都在细节上略有不同,但净效果是一样的 - 你要求它完成了在后台读取,那么你的事件派遣循环被唤醒时,I / O完成

Both are a little different in the details, but the net effect is the same -- you request a read which completes in the background, then your event dispatch loop gets woken up when the I/O finishes.

这篇关于如何亲热跨平台的异步文件I / O在C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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