7be Subj : Re: Challenge: Multithreading & Synchronization To : comp.programming.threads From : Uenal Mutlu Date : Thu May 19 2005 01:16 pm "Sergei Organov" wrote > "Uenal Mutlu" writes: > > "Sergei Organov" wrote > > > "Uenal Mutlu" writes: > > > > Can you give an example for just the member function f2(), or more if > > > > possible? > > > > > > Sorry, f2() with lock() at the beginning and unlock() at the end? Or > > > f2() in a supposed to be a good design? In the former case it's trivial, in > > > the latter case I'm afraid there will be no f2(). > > > > lock(), unlock() applied to what? > > Applied to the single recursive mutex that protects all the data inside the > Server class: > > class RecursiveMutex; > class Server > { > public: > void f2() > { > mutex.lock(); > // uses vMD, vSD > mutex.unlock(); > } > > private: > RecursiveMutex mutex; > std::vector vSD; > std::vector vJD; > std::vector vMD; > }; Hmm. so you are using just one mutex for the whole class (for all shared data objects). This is not the recommended way because you are blocking too much and too long. Imagine if inside f2() the mutex is locked, then no other thread can do anything else; all have to wait for f2() release the mutex. By using just one mutex you effectively made the program practically "single-threaded". IMO this approach is far from being efficient. Why not use 3 mutices?... > > Why would you remove f2() since it is a member function, ie. an API > > function. > > 1. Because f2() is not a good name for an API function, obviously. Understand. > 2. Because the API probably is broken to the same level as the rest of > the design. The API is just the public functions of the class, so we can say that the naming f1(), f2(), .., f9() is a bad choice, and that instead we should have taken some better names. I agree. Thank you very much. . 0