开发者

Tinyxml Multi Task

开发者 https://www.devze.com 2022-12-22 13:11 出处:网络
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:

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 pr开发者_开发百科oblem will be great. Thanks.


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 :).

0

精彩评论

暂无评论...
验证码 换一张
取 消