Subj : Re: Challenge: Multithreading & Synchronization To : comp.programming.threads From : Uenal Mutlu Date : Fri May 20 2005 03:58 pm "Maciej Sobczak" wrote > > Uenal Mutlu wrote: > > > We've usually the following cases: > [...] > > Really? > What about the multithreaded program, where all threads use stdout to > log their actions? > > puts("I'm doing this."); > puts("I'm doing that."); > > Your idea of using the Lockable template breaks here, because there is > nothing around that you could wrap with it. > > Which "case" would you apply here? You have to ask yourself especially the following question: "which object(s) am I sharing among multiple threads?" Then the solution will be simple. > But anyway - all the cases you show are very specific to the operation > with a single value object (some vector). This is not what happens in > general. > > > // case1: exclusive access to an object: > > void f() > > { > > Locker L(vec.m); > > for (size_t i = 0; i < vec.size(); i++) > > { > > // manipulate vec > > } > > } > > Let's say that I want to print the content of the vector to stdout. The > above will not help me - I do not want other functions printing anything > else in the middle (this might even crash the program). It works. What do you think does "exclusive access to the object" mean? Try this: void f() { Locker L(vec.m); for (size_t i = 0; i < vec.size(); i++) cout << vec[i].myfield << ... } .