Subj : Re: Challenge: Multithreading & Synchronization To : comp.programming.threads From : Maciej Sobczak Date : Sat May 21 2005 12:04 am Uenal Mutlu wrote: >>print("I'm doing something"); >> >>Show me how you make it lockable. Forget about streams, cout, stdout or >>any other object. > > If you cannot assign a mutex to an object (funny imagination in MT app, btw) > then use a wrapper + mutex for it. Like this one: > mutex gm; > void myprint(char* Apsz) > { > Locker L(gm); > print(Apsz); > } Cool. void f1() { myprint("First name : Maciej"); myprint("Family name: Sobczak"); myprint("Profession : Programmer"); } void f2() { myprint("First name : Cody"); myprint("Family name: Hacker"); myprint("Profession : Programmer"); } No luck, sorry. I run f1 and f2 in two threads and I got this: First name : Cody First name : Maciej Family name: Sobczak Profession : Programmer Family name: Hacker Profession : Programmer >>>Time will prove you wrong. >> >>Time's already proved that I was wrong a few times in my life, so I'm ready. > > Sorry to hear about this. Don't worry about me. At least I learned how to recognize such situations and how to benefit from them. >>>Using the "mutex per object" method gives you a simple and elegant solution: >> >>Except when there is no object? (see the print function above) > > Everything is an object. How do you achieve the "wait for something to be done"? Example: One thread moves some elements from one container to another. I want to know (in different thread) when it is done. >>>Some people will argue that just 1 mutex would be sufficient. Let them think so. >>>The above is the recommended method. >> >>Could you reference some books or articles recommending it? > > I don't know if anybody else is recommending this method, but I for sure do it. > Just do your own research --> google. No need to google. It is enough to read one of your own previous posts to see the same code using only one mutex. -- Maciej Sobczak : http://www.msobczak.com/ Programming : http://www.msobczak.com/prog/ .