Subj : Re: Challenge: Multithreading & Synchronization To : comp.programming.threads From : Uenal Mutlu Date : Fri May 20 2005 11:14 pm "Maciej Sobczak" wrote > Uenal Mutlu wrote: > > >>The terminal? > > > > Of course: > > Question: Is in your application the terminal shared by multiple threads? > > Answer: YES > > Then: make the terminal lockable > > No. I have specifically used the puts() function to show you that there > are cases where there might be no "object" which you can "physically" > grab and wrap. This example was extremely simple, yet you have rejected > to see the point and have converted the example to use cout - just to > fit it to your broken idea? > > Let's say there is no cout, but instead the "print" function: > > 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); } > Yes, you can make a class around this single function, create an object > out of it (singleton or monostate) and then make it lockable, but it is > still not the point - I can have many different "unit" actions (even > belonging to different libraries) that I want to orchestrate to achieve > some sequence that will be well-defined according to some protocol. > Mutex per object will not help me with this. If you think so, then it indeed will not help you. I was thinking you were looking for a practical solution, but it seems you are after artifical situations where it seems impossible (to you). Take my advice: there's always a way! > > How: of course by a mutex; see below > > >>Again - think in terms of actions, not in terms of objects > >>(multithreading is about "doing", not about "values"). Then you will > >>maybe discover that your concept of embedding mutexes in all value > >>objects brings you nowhere. > > > > 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. > > But, in case you really don't know: > > > > In your case actually you want to use 2 objects in a shared manner: > > 1) the output stream > > 2) the vector > > In this specific case, fine. > > > 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. > > 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. .