1

I've created a c++ program using Qt that writes to a file. It is likely this program will have multiple instances, with each instance accessing the file over a local network.

I'm using QFile::ReadWrite as my file open options. If one process opens the file in this mode, I've found that the other process can also open it for writing. I use file.write(text) to write to the file. What would happen if both processes tried to do this at exactly the same time? Does Windows handle this?

I am wondering whether to re-implement using the Window's CreateFile(...) and using 0 for the sharemode is required?

Thanks.

Coolmurr
  • 65
  • 3
  • 11
  • 1
    possible duplicate of [Accessing a single file with multiple threads](http://stackoverflow.com/questions/1632470/accessing-a-single-file-with-multiple-threads) – mrks Feb 27 '15 at 03:08
  • I am using separate processes not threads, however that was useful. I do really just want to know what `QFile::ReadWrite` allows with regards to simultaneous writing though. – Coolmurr Feb 27 '15 at 13:32

1 Answers1

3

Qt is OpenSource, so it's no problem just to look inside (qfsfileengine_win.cpp):

bool QFSFileEnginePrivate::nativeOpen(QIODevice::OpenMode openMode)
{
...
// All files are opened in share mode (both read and write).
DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
...
 // Create the file handle.
fileHandle = CreateFile((const wchar_t*)fileEntry.nativeFilePath().utf16(),
                        accessRights,
                        shareMode,
                        &securityAtts,
                        creationDisp,
                        FILE_ATTRIBUTE_NORMAL,
                        NULL);

Thus Qt provides no file-sharing functionality except "don't care".

Matt
  • 13,674
  • 1
  • 18
  • 27