Subj : Re: Challenge: Multithreading & Synchronization To : comp.programming.threads From : Uenal Mutlu Date : Thu May 19 2005 01:40 pm "Maciej Sobczak" wrote > > Uenal Mutlu wrote: > > BTW, as can been this solution also has a simple and effient method to > > make any class with a standard constructor (incl. STL classes) thread safe. > > Depends on what you mean by "thread safe". > > What you need is synchronized *collaboration* between objects. Usually > *many* objects, of possibly different types. Adding a mutex to every > value is in general operating on the wrong level of granularity. I disagree. It depends on whether your objects are dependent of each other or not; see below. > It is neither simple (you limit yourself to use only the default > constructor of the given class) That's an argument and I accept it. But OTOH IMO any class should have a default ctor, most STL classes (esp. the containers) are so. > nor efficient (you have to lock a lot of > mutexes to achieve any non-trivial collaboration between separate objects). If you see this in the context of multiple threads then IMO the approach is the most effective and fastest solution for the overall performance of the application. > > It's done like this: > > > > Lockable > myvec; > > mutex mtx; > vector vec; With my method you have the flexibility; ie you have alternatives. In some cases it makes sense to lock dependent data (ie. a group) using just one mutex. And I too would recommend to use just 1 mutex in this case because it is more efficient. But if the data objects are independent of each other then it is more efficient to let each have its own mutex. .