在不同的平台上有pread的等价物吗? [英] Are there equivalents to pread on different platforms?

查看:213
本文介绍了在不同的平台上有pread的等价物吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中编写一个并发的持久消息队列,这需要并发读取一个文件,而不使用内存映射io。简单来说,几个线程需要从文件的不同偏移量读取。

I am writing a concurrent, persistent message queue in C++, which requires concurrent read access to a file without using memory mapped io. Short story is that several threads will need to read from different offsets of the file.

最初我有一个文件对象,有典型的读/写方法,线程会获取一个互斥量来调用这些方法。然而,这样发生,我没有正确地获取互斥量在某处,导致一个线程在读/写期间移动文件偏移量,另一个线程将开始读/写到该文件的不正确的部分。

Originally I had a file object that had typical read/write methods, and threads would acquire a mutex to call those methods. However, it so happened that I did not acquire the mutex properly somewhere, causing one thread to move the file offset during a read/write, and another thread would start reading/writing to an incorrect part of the file.

所以,偏执的解决方案是每个线程有一个打开的文件句柄。现在我有很多文件句柄到同一个文件,我假设不能很好。

So, the paranoid solution is to have one open file handle per thread. Now I've got a lot of file handles to the same file, which I'm assuming can't be great.

我想使用像< a href =http://linux.about.com/library/cmd/blcmdl2%5Fpread.htm =nofollow> pread ,它允许将当前偏移量传递到读/写函数。

I'd like to use something like pread, which allows passing in of the current offset to read/write functions.

然而,这个函数只能在linux上使用,我需要在windows,aix,solaris和hpux上有相同的实现,任何建议?

However, the function is only available on linux, and I need equivalent implementations on windows, aix, solaris and hpux, any suggestions?

推荐答案

在Windows上, ReadFile()函数可以做到,参见 lpOverlapped 参数和关于异步IO的此信息

On Windows, the ReadFile() function can do it, see the lpOverlapped parameter and this info on async IO.

这篇关于在不同的平台上有pread的等价物吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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