Boost logo

Boost Users :

From: Phil Endecott (spam_from_boost_users_at_[hidden])
Date: 2005-12-31 18:01:15


Dear All,

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.

In my multithreaded application I have a struct with some uninteresting
data in it, but I want updates to the struct as a whole to be atomic so
I have a mutex. For example:

struct person {
   string firstname;
   string surname;
   mutex m;
};

I want to be able to put these structs in a container. Say a map:

map<int,person> people;

person p;
people.insert(make_pair(1,p));

But I can't do this because the mutex is noncopyable and inserting
copies it.

Yes, I could have a map<int,person*>. But I don't understand why I'm
forced to do that.

Of course, copying a struct person while the mutex is locked would be a
bad thing to do. And copying the struct is not thread safe. But
higher-level constraints can prevent this sort of thing from happening;
for example, I might create my map while only one thread exists.

Can anyone offer any insight?

Thanks,

--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