> I want to implement cout in my multithreaded program.
This cout stream is used for my debug information.
I don't want these messages to get shuffled when a thread switch takes
place.
The threads cannot be blocked, so I probably need to store cout messages
from each thread in a separate buffer.
Any leads or suggestions for this problem?
Anthony,
I posted a few days ago a solution in the comp.lang.c++.moderated
group, along with a solution for use in Windows. Basically, the trick
is to use a macro with a locking class that uses the RAII idion
(Resource Acquisition Is Initialization): it locks in the constructor
and unlocks in the destructor.
A macro could look like this:
extern SynchronizationPrimitive synch_prim;
#define CERR (Lock(synch_prim), cerr)
When you write something like:
CERR << "This is a test" << endl;
and since the return value of this expression is the rightmost value,
effectively you get something like this:
1. leftmost expression is evaluated: lock synchronization primitive
2. right-most expression is evaluated: cerr and friends to its right
3. synchronization primitive is unlocked.
I posted a complete example that I obtained from this site, books and
magazines and I posted the example in the moderated newsgroup. Search
in that newsgroup for the complete source code and let me
know--through this newsgroup--if I can be of further assistance.
Regards,
Javier.