writes:
> I have a class that creates a thread, a mutex and a condition
> variable in its constructor. I am writing a copy constructor
> for this class in C++. I am doing a simple copy using the member
> initialization list. First of all, does anyone have any opinion
> about whether this will work fine.
It will not. Mutexes and conditions are non-copyable.
They may refer to dynamically allocated memory, or they may include
addresses registered with the OS. They are typically implemented in C,
so there is no C++ copy constructor nor assignment operator defined
for them, and thus you will typically get a bitwise copy rather than
an error, which doesn't need to yield a sensible semantics however.
Besides, it's not clear what would it mean to copy them if somebody is
waitng on them, or whether a copy of a locked mutex should be locked.
They are non-copyable conceptually, not only technically.
It's possible that for a class which includes a mutex, a sensible copy
constructor should freshly initialize the mutex in the copy. Possibly
while keeping the mutex of the original locked, to prevent taking the
snapshot of data which is being modified by another thread. This all
depends on the locking policy.
Disclaimer: I've never tried to make a copyable object which includes
a mutex, this is all guessing from my head.
--
__("< Marcin Kowalczyk
\__/
^^
http://qrnik.knm.org.pl/~qrczak/