TMemIniFile 是线程安全的吗?
用户收到间歇性错误“无法创建文件“C:...\Filename.ini”。无法对打开了用户映射部分的文件执行请求的操作。”
我无法找到太多有关此错误的信息来帮助理解发生了什么。
TMemIniFile 是线程安全的吗?
A user is getting an intermittent error "Cannot create file "C:...\Filename.ini". The requested operation cannot be performed on a file with a user-mapped section open."
I haven't been able to find much about this error that helps understand what's going on.
Is TMemIniFile thread-safe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知 TMemIniFile (以及任何其他 TCustomIniFile 后代)不是线程安全的。您需要将其包装到关键部分中。
在此链接中,您可以找到线程安全的实现TCustomIniFile(理论上是由 TeamB 的 Peter Below 编写的,尽管我不能保证这一点)。
Embarcadero 论坛中也有关于 TMemIniFile 线程安全性的讨论 此处。他们谈论的是该组件的 C++ 版本。
您还可以在 MSDN 中找到有关错误消息来源的讨论 这里。它有点长,但总体结论是有 2 个 exe 试图访问同一个文件。您可以在此处找到有关该主题的其他讨论。
As far as I know TMemIniFile (and any other TCustomIniFile descendants) is not thread-safe. You will need to wrap it into a critical section.
In this link you can find an implementation of a thread-safe TCustomIniFile (theoretically programmed by Peter Below of TeamB though I can not assure it).
There is also a discussion in the Embarcadero forums about the thread-safety of TMemIniFile here. They speak about the C++ version of the component.
You can also find a discussion in MSDN regarding the origin of your error message here. It is a bit long but the general conclussion is that there are 2 exes trying to acces the same file. You can find another discussion on the subject here.
首先,我假设导致报告问题的特定线程配置是存在多个 TMemIniFile 实例,甚至可能在不同的进程中,从不同的线程同时保存。
TMemIniFile
不是线程安全的。为了避免任何竞争条件,您需要编写如下(伪)代码:仅锁定文件操作是不够的,因为这样您可能会因竞争而丢失更改。您必须锁定整个读/修改/写周期。
First of all, I am assuming that the particular threading configuration that is causing the problem reported is that there are multiple
TMemIniFile
instances, possibly even in different processes, being saved simultaneously from different threads.TMemIniFile
is not thread-safe. To avoid any race conditions you need to write (pseudo-)code like this:It's not enough to lock around just the file operations because then you may lose changes due to a race. You have to lock the entire read/modify/write cycle.
解决方案
此错误消息是由 AVG 中的错误引起的。显然,在实时文件访问监控模式下,AVG 以独占模式打开一些文件,防止其他人写入这些文件。
修复方法是将 AVG 配置为忽略该文件所在的文件夹。
谢谢,AVG!我现在可以把这两天浪费的时间拿回来吗? :-)
感谢所有在这里回答的人。
汤姆
Solution
This error message is caused by a bug in AVG. Apparently in its real-time file-access monitoring mode, AVG opens some files in exclusive mode, preventing others from writing to them.
The fix was to configure AVG to ignore the folder in which this file resides.
Thanks, AVG! Can I have my 2 days of wasted time back now? :-)
Thanks to everyone who answered here.
Tom