That depends on what you’re trying to do, really. This is a question specific to your usage at the end of the day. You can’t corrupt the I/O manager’s internal structures by doing this, but if you aren’t careful, you can screw up the contents of your file.
If you are always supplying an explicit file offset to ZwWriteFile/ZwReadFile, and you are handling any coherency issues with respect to multiple inflight writes to the contents of the file itself, you may not need to synchronize access. Similarly, if you opened the file in append mode and each file write is an independent ‘packet’, you will get atomic append behavior (two inflight writes are guaranteed not to be interleaved).
In other situations, if you are relying on the I/O manager’s implicit file offset for ZwWriteFile/ZwReadFile, you need to be careful not to corrupt your file due to the file offset being unexpectedly set by multiple read or write operations. For example, if the data that you write to the file is not specified as a single atomic unit in a call to ZwWriteFile but is interspersed across several calls, it might not be safe to assume that the implicit file offset moves sequentially forward in the face of multiple threads working on the same file handle simultaneously.
See the documentation for ZwWriteFile and the ByteOffset parameters for some further discussion.
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Andrei Belogortseff
Sent: Sunday, September 12, 2010 10:49 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] Calling ZwWriteFile/ZwReadFile from multiple threads?
I was hoping my question was easy enough to find the answer in the WDK docs or on the web, but after searching for a couple of hours, I didn’t come across anything reliable. Here is my last hope to find it out 
A driver creates a file (with ZwCreateFile, specifying FILE_SYNCHRONOUS_IO_NONALERT in the CreateOptions), and also creates several threads reading and writing to/from the file (with ZwWriteFile/ZwReadFile).
The question is: does the driver need to synchronize access to the file handle, or do ZwWriteFile/ZwReadFile synchronize such access internally?
Does the answer depend on the CreateOptions such as FILE_WRITE_THROUGH and FILE_NO_INTERMEDIATE_BUFFERING?
Thank you for the advice.
A.
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer