NoSenseEtAl
NoSenseEtAl

Reputation: 29996

explicitly calling ~boost::lock_guard<> causes mutex deadlock

if this is a bug I have no problem just not doing it, but if this is an expected behaviour I would like to know why.

I do something like this:

{
   boost::lock_guard<boost::mutex> lg(tagsToSocketsMtx);
// mutex protected work 
   lg.~lock_guard(); // this causes deadlocks later(combined with ...
  //...other uses of the same mtx, ofc I use different lock guard in other functions)

// rest of the function
}

Upvotes: 2

Views: 602

Answers (1)

JoeG
JoeG

Reputation: 13182

Once the construction of lg completes, C++ guarantees that its destructor will be called on scope exit regardless of the fact that you're are also making an explicit destructor call.

By destroying lg twice, you are invoking undefined behaviour, and in this case the bug manifests as a deadlock.

Upvotes: 2

Related Questions