0

for TinyXML is a good XMl libuary, i use it to save packet data in network transmission, such as the client receive some packet from the server in UDP muticast mode. client join more than one muticast groups, so it must create multi-thread to receive and write the data in different files(of course, the numbers of file equals the nums of muticast groups). i design a writeXML class which has a DoWrite(char*,size_t) function.

such as :

void DoWrite(char*,size_t)
{
boost::unique_lock<boost::mutex> lLock(m_lock);
lLock.lock();
}

but the problem is whenever the DoWrite function is called, the boost:lock_error comes. who can help me? tks very much! emphasized text

user909691
  • 25
  • 1
  • 1
  • 8
  • If you post a question about errors, you should always include those errors in the question. Please edit your question to include the errors. – Some programmer dude Sep 04 '12 at 06:13
  • However, I would venture a guess that it point out that there is no variable `m_lock` defined. It looks like `DoWrite` is a stand-alone function, and `m_lock` is named in a way that is typical for class members. Either you forgot the class-specifier, or it's inline in a class in which case you should probably show that a little better. – Some programmer dude Sep 04 '12 at 06:16

1 Answers1

1

Remove lLock.lock(); from your code, boost::unique_lock calls lock on the passed in mutex for you, so you do not need to call it manually (you are calling lock twice on the mutex). From the reference:

unique_lock(Lockable & m)

Effects:

Stores a reference to m. Invokes m.lock().

Also, use boost::lock_guard instead unless you want to defer acquiring the lock. See the documentation for more details.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166