Tinyxml 多任务

发布于 2024-08-24 13:17:47 字数 507 浏览 10 评论 0原文

我有一个 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中的数据,但无法写入。

我考虑了两种使其工作的可能性,但我不知道如何实现它们:

  1. 当我完成它时销毁 doc 对象。
  2. 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:

  1. Destroy the doc object when I'm done with it.
  2. Some function in Tinyxml library to unload the file.

Any help or better understanding of the problem will be great.
Thanks.

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

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

发布评论

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

评论(1

倥絔 2024-08-31 13:17:47

根据评论更新(废弃之前的答案):

好吧,我在TinyXml文档中没有看到太多告诉我们如何在不限制其他线程的情况下打开文档的内容。

在这种情况下,您应该做的是仅向 TiXmlDocument 声明一个实例并在线程之间共享它。每当线程写入文件时,它都会进入临界区,写入需要写入的内容,保存文件,然后退出临界区。

我没有看到其他解决方法。

根据评论更新:
由于您使用的是 MFC 线程,您的代码应该如下所示:

class SafeTinyXmlDocWrapper
{
private:
    static bool m_isAlive = FALSE;
    static CCriticalSection m_criticalSection;
    char* m_xmlFileName;
    TiXmlDocument m_doc;

public:

    SafeTinyXmlDocWrapper()
    {
        m_xmlFileName = "C:\\browsarityXml.xml";
        m_doc = TiXmlDocument(m_xmlFileName);
        m_doc.LoadFile();
        m_isAlive = TRUE;
    }

    ~SafeTinyXmlDocWrapper()
    {
        CSingleLock lock(&m_criticalSection);
        lock.Lock(); // only one thread can lock

        m_isAlive = FALSE;

        // cleanup and dispose of the document

        lock.Unlock();
    }

    void WriteBatch(BatchJob& job)
    {
        CSingleLock lock(&m_criticalSection);
        lock.Lock(); // only one thread can lock
        if(m_isAlive) // extra protection in case the destructor was called
        {
            // write the batch job to the xml document

            // save the xml document
            m_doc.SaveFile(m_xmlFileName);
        }
        lock.Unlock(); // the thread unlocks once it's done
    }
}

我有一段时间没有编写 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:

class SafeTinyXmlDocWrapper
{
private:
    static bool m_isAlive = FALSE;
    static CCriticalSection m_criticalSection;
    char* m_xmlFileName;
    TiXmlDocument m_doc;

public:

    SafeTinyXmlDocWrapper()
    {
        m_xmlFileName = "C:\\browsarityXml.xml";
        m_doc = TiXmlDocument(m_xmlFileName);
        m_doc.LoadFile();
        m_isAlive = TRUE;
    }

    ~SafeTinyXmlDocWrapper()
    {
        CSingleLock lock(&m_criticalSection);
        lock.Lock(); // only one thread can lock

        m_isAlive = FALSE;

        // cleanup and dispose of the document

        lock.Unlock();
    }

    void WriteBatch(BatchJob& job)
    {
        CSingleLock lock(&m_criticalSection);
        lock.Lock(); // only one thread can lock
        if(m_isAlive) // extra protection in case the destructor was called
        {
            // write the batch job to the xml document

            // save the xml document
            m_doc.SaveFile(m_xmlFileName);
        }
        lock.Unlock(); // the thread unlocks once it's done
    }
}

I have not written C++ in some time now, but it should be roughly what you're looking for. Bells and whistles cost extra :).

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