Are there equivalents to pread on different platforms?

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.

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

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上的此信息。


With NIO, java.nio.channels.FileChannel has a read(ByteBuffer dst, long position) method, which internally uses pread .

Oh wait, your question is about C++, not Java. Well, I just looked at the JDK source code to see how it does it for Windows, but unfortunately on Windows it isn't atomic: it simply seeks, then reads, then seeks back.

For Unix platforms, the punchline is that pread is standard for any XSI-supporting (X/Open System Interface, apparently) operating system: http://www.opengroup.org/onlinepubs/009695399/functions/pread.html


Based on another answer, the closest I could come up with is this. However, there is a bug: ReadFile will change the file offset, and pread is guaranteed to not change the file offset. There's no real way to fix this, because code can do normal read() and write() concurrently with no lock. Anybody found a call that will not change the offset?

unsigned int FakePRead(int fd, void *to, std::size_t size, uint64_offset) {
  // size_t might be 64-bit.  DWORD is always 32.
  const std::size_t kMax = static_cast<std::size_t>(1UL << 31);
  DWORD reading = static_cast<DWORD>(std::min<std::size_t>(kMax, size));
  DWORD ret;
  OVERLAPPED overlapped;
  memset(&overlapped, 0, sizeof(OVERLAPPED));
  overlapped.Offset = static_cast<DWORD>(off);
  overlapped.OffsetHigh = static_cast<DWORD>(off >> 32);
  if (!ReadFile((HANDLE)_get_osfhandle(fd), to, reading, &ret, &overlapped)) {
    // TODO: set errno to something?
    return -1;
  }
  // Note the limit to 1 << 31 before.
  return static_cast<unsigned int>(ret);
}
链接地址: http://www.djcxy.com/p/19752.html

上一篇: 平台和交叉

下一篇: 是否有相当于在不同平台上进行预编译?