Subj : Re: Challenge: Multithreading & Synchronization To : comp.programming.threads From : Uenal Mutlu Date : Sat May 21 2005 12:32 am "Maciej Sobczak" wrote > 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 Simple solution: Add this at top of your two functions f1() and f2(): Locker L(gm); And leave myprint() as currently is. But, your mutex must be of recursive type. > 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. I personally do use an atomic flag (actually it is an atomic counter with the possible values 0 and 1). I've this implemented in a class. Internally it uses InterLockedXXX funcs of the Win32 API. There are many alternatives. The simplest is: volatile bool gMyFlag = false; BUT: this method is not recommended. .