iostreams in multithreaded environment

A

Anthony

Hi,



How does (Can?) the stream mechanism work in multithreaded programs?



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?



Greetings Anthony
 
V

Victor Bazarov

Anthony said:
How does (Can?) the stream mechanism work in multithreaded programs?

It is unspecified by the Standard. You can probably find more info
in the documentation to your compiler.
I want to implement cout in my multithreaded program.

That statement is unclear. 'cout' is the name of a standard output
stream. It is implemented by the vendor of the standard library
you're using. You shouldn't have to implement it yourself. Perhaps
you mean that you want to do some outputting in every thread...
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?

Every thread should have its own _stringstream_ for output and
then stuff it into 'cout' right before finishing its work. That
will assure that all output of each thread will be in one place.

Unfortunately, that means that you cannot use 'std::cout << ...'
to output your debug information, but you should be able to write
some kind of a macro to do your debugging output (which would
also allow you to disable the output when done debugging) with
the help of the thread ID or some such.

Since C++ and multithreading are orthogonal (the Standard does
not define anything thread-related), you might find more useful
information in comp.programming.threads.

Victor
 
T

tom_usenet

Hi,



How does (Can?) the stream mechanism work in multithreaded programs?

By appropriately locking access to shared streams.
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?

Log through a proxy that locks a mutex. e.g.

log(WARNING).get() << "This appears" << " then this" <<
" regardless of other threads using the log.\n";

(or use
#define LOG(level) log(level).get() << __FILE__ << ':' << __LINE__
or similar).

The log function will return a proxy object that locks a mutex in the
constructor and unlocks it in the destructor when the last reference
is destroyed (using reference counting).

I believe James Kanze has such a logging library:
http://www.gabi-soft.fr/codebase-en.html

Tom
 
J

Javier Estrada

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top