Tinyxml 多任务
我有一个 xml 文件,程序 (BHO) 的每个新线程都使用相同的 Tinyxml 文件。
每次在程序中打开一个新窗口时,它都会运行以下代码:
const char * xmlFileName = "C:\\browsarityXml.xml";
TiXmlDocument doc(xmlFileName);
doc.LoadFile();
//some new lines in the xml.. and than save:
doc.SaveFile(xmlFileName);
问题是,在第一个窗口将新数据添加到xml并保存后,下一个窗口无法添加到它。虽然下一个可以读取xml中的数据,但无法写入。
我考虑了两种使其工作的可能性,但我不知道如何实现它们:
- 当我完成它时销毁 doc 对象。
- Tinyxml 库中的一些函数用于卸载文件。
任何帮助或更好地理解问题都会很棒。 谢谢。
I have a single xml file and every new thread of the program (BHO) is using the same Tinyxml file.
every time a new window is open in the program, it runs this code:
const char * xmlFileName = "C:\\browsarityXml.xml";
TiXmlDocument doc(xmlFileName);
doc.LoadFile();
//some new lines in the xml.. and than save:
doc.SaveFile(xmlFileName);
The problem is that after the first window is adding new data to the xml and saves it, the next window can't add to it. although the next one can read the data in the xml, it can't write to it.
I thought about two possibilities to make it work, but I don't know how to implement them:
- Destroy the doc object when I'm done with it.
- Some function in Tinyxml library to unload the file.
Any help or better understanding of the problem will be great.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据评论更新(废弃之前的答案):
好吧,我在TinyXml文档中没有看到太多告诉我们如何在不限制其他线程的情况下打开文档的内容。
在这种情况下,您应该做的是仅向
TiXmlDocument
声明一个实例并在线程之间共享它。每当线程写入文件时,它都会进入临界区,写入需要写入的内容,保存文件,然后退出临界区。我没有看到其他解决方法。
根据评论更新:
由于您使用的是 MFC 线程,您的代码应该如下所示:
我有一段时间没有编写 C++ 了,但它应该大致是您正在寻找的内容。附加功能需要额外付费:)。
Update based on comments (scrap previous answer):
OK, I didn't see much in the TinyXml documentation that tells us how to open a document without restriction to other threads.
What you should do in this case is declare only one instance to the
TiXmlDocument
and share it between threads. Whenever a thread writes to the file it will enter a critical section, write what it needs to write, save the file, then exit the critical section.I don't see another workaround.
Update per comments:
Since you're using MFC threads your code should look something like this:
I have not written C++ in some time now, but it should be roughly what you're looking for. Bells and whistles cost extra :).