question

drjackool-3839 avatar image
0 Votes"
drjackool-3839 asked DavidLowndes-6766 edited

Writing a file by multi-thread MFC

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

vs-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DavidLowndes-6766 avatar image
0 Votes"
DavidLowndes-6766 answered DavidLowndes-6766 edited

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thanks what about mutex do I need or not?

0 Votes 0 ·

I don't see a need for a mutex if there's nothing common shared.
Give it a try and see if it works OK.

0 Votes 0 ·