Are there equivalents to pread on different platforms?

后端 未结 3 396
梦毁少年i
梦毁少年i 2021-01-19 11:20

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 threa

3条回答
  •  孤独总比滥情好
    2021-01-19 11:46

    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(1UL << 31);
      DWORD reading = static_cast(std::min(kMax, size));
      DWORD ret;
      OVERLAPPED overlapped;
      memset(&overlapped, 0, sizeof(OVERLAPPED));
      overlapped.Offset = static_cast(off);
      overlapped.OffsetHigh = static_cast(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(ret);
    }
    

提交回复
热议问题