Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 815
Snazzer Avatar asked Apr 20 '09 00:04

Snazzer


2 Answers

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

like image 164
jpalecek Avatar answered Sep 18 '22 22:09

jpalecek


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

like image 29
Chris Jester-Young Avatar answered Sep 19 '22 22:09

Chris Jester-Young