QSettings - 两个进程之间的同步问题

发布于 2024-12-02 00:18:23 字数 1512 浏览 3 评论 0原文

我正在将 Qsettings 用于非 gui 产品,将其设置存储到 xml 文件中。这是作为一个库编写的,可在 C、C++ 程序中使用。每个产品都会有 1 个 xml 文件。每个产品可能有多个子产品,它们按子产品分组写入 xml,如下 -

文件:“product1.xml”

<product1>
    <subproduct1>
       <settings1>..</settings1>
       ....
       <settingsn>..</settingsn>
    </subproduct1>
    ...
    <subproductn>
       <settings1>..</settings1>
       ....
       <settingsn>..</settingsn>
    </subproductn>

 </product1>

文件:productn.xml

<productn>
   <subproduct1>
       <settings1>..</settings1>
       ....
       <settingsn>..</settingsn>
   </subproduct1>
   ...
   <subproductn>
       <settings1>..</settings1>
       ....
       <settingsn>..</settingsn>
   </subproductn>

 </productn>

中的代码一个进程执行以下操作 -

settings = new QSettings("product1.xml", XmlFormat);
settings.setValue("settings1",<value>)
sleep(20);
settings.setValue("settings2", <value2>)
settings.sync();

当第一个进程进入睡眠状态时,我启动另一个执行以下操作的进程 -

settings = new QSettings("product1.xml", XmlFormat);
settings.remove("settings1")
settings.setValue("settings3", <value3>)
settings.sync();

我希望 settings1 远离product1.xml 文件,但它仍然保留在文件中 - 最后的product1.xml以上两个过程。我没有在我的设置库中使用 QCoreApplication(..) 。以上设计如有问题请指出。

I am using Qsettings for non gui products to store its settings into xml files. This is written as a library which gets used in C, C++ programs. There will be 1 xml file file for each product. Each product might have more than one sub products and they are written into xml by subproduct grouping as follows -

File: "product1.xml"

<product1>
    <subproduct1>
       <settings1>..</settings1>
       ....
       <settingsn>..</settingsn>
    </subproduct1>
    ...
    <subproductn>
       <settings1>..</settings1>
       ....
       <settingsn>..</settingsn>
    </subproductn>

 </product1>

File: productn.xml

<productn>
   <subproduct1>
       <settings1>..</settings1>
       ....
       <settingsn>..</settingsn>
   </subproduct1>
   ...
   <subproductn>
       <settings1>..</settings1>
       ....
       <settingsn>..</settingsn>
   </subproductn>

 </productn>

The code in one process does the following -

settings = new QSettings("product1.xml", XmlFormat);
settings.setValue("settings1",<value>)
sleep(20);
settings.setValue("settings2", <value2>)
settings.sync();

When the first process goes to sleep, I start another process which does the following -

settings = new QSettings("product1.xml", XmlFormat);
settings.remove("settings1")
settings.setValue("settings3", <value3>)
settings.sync();

I would expect the settings1 to go away from product1.xml file but it still persist in the file - product1.xml at the end of above two process. I am not using QCoreApplication(..) in my settings library. Please point issues if there is anything wrong in the above design.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈情不如逗狗 2024-12-09 00:18:23

这是您正在做的一件奇怪的事情,但需要注意的一件事是,sync() 调用实际上是将文件写入磁盘的。在这种情况下,如果您希望第二个进程实际看到您所做的更改,那么您需要在第二个进程访问文件之前调用sync(),以保证它会实际上看到你的修改。因此,我会尝试在 sleep(20) 之前调用 settings.sync()

This is kind of an odd thing that you're doing, but one thing to note is that the sync() call is what actually writes the file to disk. In this case if you want your second process to actually see the changes you've made, then you'll need to call sync() before your second process accesses the file in order to guarantee that it will actually see your modifications. Thus I would try putting a settings.sync() call right before your sleep(20)

懒猫 2024-12-09 00:18:23

也许你必须在 sync() 之后执行 delete settings; 以确保它没有打开,然后在其他进程中进行写入?

Maybe you have to do delete settings; after the sync() to make sure it is not open, then do the writing in the other process?

倾城花音 2024-12-09 00:18:23

这能编译吗?您使用什么 XmlFormat 实现以及哪个操作系统?您的项目中必须有一些特殊的代码用于存储/读取 Xml - 此代码中必须有某些内容与您期望的工作方式不同。

Does this compile? What implementation of XmlFormat are you using and which OS? There must be some special code in your project for storing / reading to and from Xml - there must be something in this code which works differently from what you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文