views:

318

answers:

2

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?

+1  A: 

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

Chris Jester-Young
+2  A: 

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

jpalecek
Nice. I wonder why the JDK doesn't (appear to) use that....
Chris Jester-Young
Looks like this would work, seems that the overlapped parameter is still used even for synchronous file access. Thanks jpalecek!
Snazzer