Boost logo

Boost Users :

From: Phil Endecott (spam_from_boost_users_at_[hidden])
Date: 2006-01-01 13:05:55


>>Can someone help me to understand why boost::thread::mutex is
>>noncopyable? I expect that there is a good reason, but I can't see it
>>at the moment.
>
> http://www.boost.org/doc/html/threads/faq.html#id2786688

Thanks for the link. Here is the text:

Why do Mutexes have noncopyable semantics?

To ensure that deadlocks don't occur. The only logical form of copy
would be to use some sort of shallow copy semantics in which multiple
mutex objects could refer to the same mutex state. This means that if
ObjA has a mutex object as part of its state and ObjB is copy
constructed from it, then when ObjB::foo() locks the mutex it has
effectively locked ObjA as well. This behavior can result in deadlock.
Other copy semantics result in similar problems (if you think you can
prove this to be wrong then supply us with an alternative and we'll
reconsider).

I disagree with the first comment: "The only logical form of copy would
be to use some sort of shallow copy semantics in which multiple mutex
objects could refer to the same mutex state." What's wrong with the
copy of a mutex being a new independent mutex? (There's a problem if
you copy a locked mutex, but I don't propose to do that.)

--Phil.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net