Writing a file by multi-thread MFC

drjackool 956 Reputation points
2021-09-04T08:10:24.04+00:00

Hi
I have several threads that them write into same binary file but each thread writes a specific range of file and the ranges is not overlapped.
For example:
thread #1 byte 0 to 100
thread #2 byte 101 to 200
ans so on...

Now how I implement it, do I need use mutex? and for the file handle can I simply share file handle between threads or should use DublicateHandle?

Thanks

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,887 questions
0 comments No comments
{count} votes

Accepted answer
  1. David Lowndes 4,711 Reputation points
    2021-09-04T08:25:30.823+00:00

    The remarks section for the DuplicateHandle documentation say it won't allow each handle to have a different position:

    For example, if you duplicate a file handle, the current file position is always the same for both handles. For file handles to have different file positions, use the CreateFile function to create file handles that share access to the same file.

    So, for what you want to do, I think you'd need individual handles from CreateFile.


0 additional answers

Sort by: Most helpful