Note that there are some explanatory texts on larger screens.

plurals
  1. POConsistency of two C FILE* streams on a single file
    text
    copied!<p>I need to implement a simple "spill to disk" layer for large volume of data coming off a network socket. I was hoping to have two C <code>FILE*</code> streams, one used by a background thread writing to the file, one used by a front end thread reading it.</p> <p>The two streams are so one thread can be writing at one offset, while the other is reading elsewhere - without taking a lock and blocking the other thread.</p> <p>There will be a paging mechanism so the reads/writes are at random access locations - not necessarily sequential.</p> <p>One more caveat, this needs to work on Windows and Linux.</p> <p>The question: after the fwrite to the first stream has returned, is that written data guaranteed to be immediately visible to an fread on the second stream?</p> <p>If not, what other options might I consider?</p> <hr> <p>So Posix pread/pwrite functions turned out to be what I needed. Here's a version for Win32:</p> <pre><code>size_t pread64(int fd, void* buf, size_t nbytes, __int64 offset) { OVERLAPPED ovl; memset(&amp;ovl, 0, sizeof(ovl)); *((__int64*)&amp;ovl.Offset)=offset; DWORD nBytesRead; if (!ReadFile((HANDLE)_get_osfhandle(fd), buf, nbytes, &amp;nBytesRead, &amp;ovl)) return -1; return nBytesRead; } size_t pwrite64(int fd, void* buf, size_t nbytes, __int64 offset) { OVERLAPPED ovl; memset(&amp;ovl, 0, sizeof(ovl)); *((__int64*)&amp;ovl.Offset)=offset; DWORD nBytesWritten; if (!WriteFile((HANDLE)_get_osfhandle(fd), buf, nbytes, &amp;nBytesWritten, &amp;ovl)) return -1; return nBytesWritten; } </code></pre> <p>(And thank you everyone for input on this - much appreciated).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload