Subj : Re: stl & threads To : comp.programming.threads From : Torsten Robitzki Date : Tue May 24 2005 08:11 pm Uenal Mutlu wrote: > "Lyonya" wrote > > And by using the following you can extend any object with a default > contructor to a 'Lockable' object, for example all STL containers: > > template class Lockable : public T > { > public: > mutex mx; // a mutex for the object > Lockable() {} > private: > Lockable(const Lockable&); // copy ctor: prohibit copying > }; The mutex should prohibit the copying of this wrapper already or the mutex implementation is already broken. > Then, in applic code one would use something like the following > automatic helper object to do the actual locking/unlocking: > > class Locker > { > public: > Locker(mutex& Amx) : mx(Amx) { mx.Lock(); } > ~Locker() { mx.Unlock(); } > private: > mutex& mx; > }; Here copying and assigning should be disabled or you can end up with doubled calls to Unlock(). But reuse is always better then rewrite: Example usage: std::pair > vec; void* thrprocA(void* Ap) { Locker L(vec.first); // it is safe to modify vec.second } sorry couldn't resist regards Torsten .