如何保证异步写入 Windows 下文件的顺序?
How can I guarantee the order of async writes to file under Windows?
给出这个伪代码:
- 打开文件进行异步访问
- 写
Value1
到文件结束位置
- 在与 2.
相同的线程中将 Value2
写入文件末尾
我可以确定 Value1
将 总是 在 Value2
之前吗?如何避免竞争条件?
Can I be sure that Value1 will be -allays- before Value2, and that Value2 will not overwrite Value1
没有。 Win32 不做这样的保证。
通常的做法是通过在 OVERLAPPED
实例的字段中指定开始写入的位置来自行管理写入位置。 IE。您确保先完成哪个写入无关紧要。
请参阅 WriteFile
文档中的 同步和文件位置 。
给出这个伪代码:
- 打开文件进行异步访问
- 写
Value1
到文件结束位置 - 在与 2. 相同的线程中将
Value2
写入文件末尾
我可以确定 Value1
将 总是 在 Value2
之前吗?如何避免竞争条件?
Can I be sure that Value1 will be -allays- before Value2, and that Value2 will not overwrite Value1
没有。 Win32 不做这样的保证。
通常的做法是通过在 OVERLAPPED
实例的字段中指定开始写入的位置来自行管理写入位置。 IE。您确保先完成哪个写入无关紧要。
请参阅 WriteFile
文档中的 同步和文件位置 。