Simultaneously and asynchronously write to file with MSVC?

I am currently using one stream and fseek / fwrite to save data to a large file in a loop. But of course saving is a bottleneck. To remove this bottleneck, I think I can create a thread to write the file asynchronously, but that will block access to the file in the next iterative thread.

Is there a way to write to a file concurrently and asynchronously in Visual C / C ++?

(OS - Windows)

+3


source to share


3 answers


Use CreateFile (), SetFilePointer () and SetEndOfFile () to create and preformat the file. Then use CreateFile () in your worker threads to open additional file descriptors, SetFilePointer () to find the offsets you want, and WriteFile () to write to it. As long as each call to CreateFile () grants FILE_SHARE_WRITE permissions, you can open multiple descriptors with GENERIC_WRITE permissions at a time. Each descriptor stores its own pointer in the file.



+1


source


Use the function:

CreateFileMapping

      

http://msdn.microsoft.com/en-us/library/windows/desktop/aa366537(v=vs.85).aspx



to memory maps the output file to memory and then you can write multiple threads to memory at the same time.

More on memory mapped files here:

http://en.wikipedia.org/wiki/Memory-mapped_file

+2


source


Asynchronous I / O functions such as WriteFileEx allow concurrent access. But it's not clear to me how you think this will improve your bandwidth.

+1


source







All Articles