Hi there,
we have a C++ class which basically reads and writes vectors from a binary file. An exemplary read function that loads a single vector into memory looks like this:
int load (const __int64 index, T* values) const {
int re = _fseeki64(_file, index * _vectorSize + _offsetData, SEEK_SET);
assert(re == 0);
size_t read = fread(values, sizeof(T), _vectorElements, _file);
assert(read == _vectorElements);
return 0;}
Out programs are multithreaded with OpenMP and multiple threads access the same file at the same time. To avoid issues because of multiple threads we always cover the function call within an OpenMP critical statement:
#pragma omp critical {
load(...);
}
I know that the Microsoft Visual C++ runtime contains several functions like _fseek_nolock
, _fread_nolock
, _fwrite_nolock
and so on... For example the _fread_nolock()
function is described as
This function is a non-locking version of fread. It is identical to fread except that it is not protected from interference by other threads. It might be faster because it does not incur the overhead of locking out other threads. Use this function only in thread-safe contexts such as single-threaded applications or where the calling scope already handles thread isolation.
Now my question: I understand that the function blocks "re-entrant" calls, so no other thread will enter the function before other threads have returned. However, I do not understand why it is necessary to protect a single function in that way. IMHO all functions that access/modify the file pointer (_file
in the code sample) must be protected and therefore be made thread-safe. This requires to build a lock around the whole function block that actuall calls the standard C functions fseek and fread, so I do not see the point of providing such non-blocking functions.
Can someone explain me these locking mechanisms because I suppose our paranoid locking scheme wastes some performance?
Thank you in advance!