0

I've two threads, one that writes to a file, and one that reads from the same file. I use CreateFile & WriteFile, FlushFileBuffers in the writer thread, and the reader thread uses FtpPutFile.

How do I implement a simple windows32 Mutex here? (I haven't used it for years)

Or, is this possible with proper flags sent to CreateFile? (I tried but no luck)

3
  • What's the problem you're trying to solve? Is it that one thread will mess up the file position for the other? Commented Aug 9, 2012 at 13:04
  • 1
    Give us more details, please. Better yet, reader & writer code (or pseudocode). Right now it's impossible to answer.
    – atzz
    Commented Aug 9, 2012 at 13:04
  • The problem is that while the file is open in the writer thread, FtpPutFile fails to upload the file. Commented Aug 9, 2012 at 13:47

2 Answers 2

1

You don't need a mutex. You can safely read and write to the same file in multiple threads. You can open the file more than once so that each thread has its own file handle and thus its own file pointer or you can use overlapped I/O to read or write at a specific position.

You don't need to open the file for overlapped I/O. You can just do this:

LARGE_INTEGER off = offset;
OVERLAPPED ov = { 0 };
ov.Offset = off.LowPart;
ov.OffsetHigh = off.HighPart;
ReadFile(handle, buffer, count, &bytesRead, &ov);

This allows you to read at a particular position in the file (offset) atomically, so you don't have to call SetFilePointerEx and worry about another thread messing up the pointer. The same thing works for writing.

8
  • Yes, you are right but the problem is that I am using a high level function and I have no control nor any idea how it opens the file for reading: FtpPutFile. Commented Aug 9, 2012 at 13:50
  • 1
    Then give that function its own handle to the file. If you need the file to be in a consistent state though, you will need to enforce that through program logic, possibly using a mutex but most likely not. (A mutex should only be held briefly while a resource is accessed. Not held over network I/O for large numbers of consecutive I/O accesses.) Commented Aug 9, 2012 at 13:52
  • The network I/O is very small. FtpPutFile receives a string containing the path of the local file to upload, not a handle to the file. Commented Aug 9, 2012 at 13:55
  • If you give it the path, then it must open the file itself. So the only issue will be making sure the file remains consistent while FtpPutFile is accessing it so you don't put garbage on the server. Commented Aug 9, 2012 at 14:27
  • 1
    @4aRkKn1gh7 I wouldn't suggest using a mutex in that case because you may deadlock with the device. Imagine if your read logic holds the mutex, but the other side isn't going to write anything until it reads the thing that your write thread can't write because it doesn't hold the mutex! Commented Sep 30, 2016 at 5:24
0

How to implement a simple mutex:
You create a mutex with the CreateMutex function OR CreateMutexEx. You will need to call CloseHandle when you are done with it. Once you have the mutex then you can call WaitForSingleObject to synchronize your calls (don't forget to call ReleaseMutex when you are done. Lastly, here is an example of using the Mutex object.

2
  • @user1581390 - Just note David's comment, minimize the length of time you hold a lock.
    – pstrjds
    Commented Aug 9, 2012 at 14:00
  • Already done that. The thing is I am trying to optimize the size of the exe and I would rather not add any more #includes or use a Mutex unless I absolutely must do so. Commented Aug 9, 2012 at 14:07

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.