Note that there are some explanatory texts on larger screens.

plurals
  1. PONon-threadsafe file I/O in C/C++
    text
    copied!<p>While troubleshooting some performance problems in our apps, I found out that C's <code>stdio.h</code> functions (and, at least for our vendor, C++'s <code>fstream</code> classes) are threadsafe. As a result, every time I do something as simple as <code>fgetc</code>, the RTL has to acquire a lock, read a byte, and release the lock.</p> <p>This is not good for performance.</p> <p>What's the best way to get non-threadsafe file I/O in C and C++, so that I can manage locking myself and get better performance?</p> <ul> <li>MSVC provides <a href="http://msdn.microsoft.com/en-us/library/xt5h2ba7.aspx" rel="noreferrer"><code>_fputc_nolock</code></a>, and GCC provides <a href="http://linux.die.net/man/3/unlocked_stdio" rel="noreferrer"><code>unlocked_stdio</code></a> and <a href="http://linux.die.net/man/3/flockfile" rel="noreferrer"><code>flockfile</code></a>, but I can't find any similar functions in my compiler (CodeGear C++Builder).</li> <li>I could use the raw Windows API, but that's not portable and I assume would be slower than an unlocked fgetc for character-at-a-time I/O.</li> <li>I could switch to something like the <a href="http://apr.apache.org/" rel="noreferrer">Apache Portable Runtime</a>, but that could potentially be a lot of work.</li> </ul> <p>How do others approach this?</p> <p><strong>Edit</strong>: Since a few people wondered, I had tested this before posting. <code>fgetc</code> doesn't do system calls if it can satisfy reads from its buffer, but it does still do locking, so locking ends up taking an enormous percentage of time (hundreds of locks to acquire and release for a single block of data read from disk). Not doing character-at-a-time I/O would be a solution, but C++Builder's <code>fstream</code> classes unfortunately use fgetc (so if I want to use <code>iostream</code> classes, I'm stuck with it), and I have a lot of legacy code that uses <code>fgetc</code> and friends to read fields out of record-style files (which would be reasonable if it weren't for locking issues).</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