Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > iostreams in multithreaded environment

Reply
Thread Tools

iostreams in multithreaded environment

 
 
Anthony
Guest
Posts: n/a
 
      10-08-2003
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







 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-08-2003
"Anthony" <(E-Mail Removed)-is-A.A-is-E.nl> wrote...
> 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


 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      10-08-2003
On Wed, 8 Oct 2003 14:45:32 +0200, "Anthony"
<(E-Mail Removed)-is-A.A-is-E.nl> wrote:

>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
 
Reply With Quote
 
Javier Estrada
Guest
Posts: n/a
 
      10-08-2003
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sleep in a multithreaded environment Me Me Ruby 5 06-30-2008 11:14 AM
open/fopen read/fread in multithreaded environment. Sheth Raxit C Programming 1 11-23-2007 07:07 AM
Question about STL containers in multithreaded environment nicolas.michel.lava@gmail.com C++ 9 03-06-2006 02:44 PM
static member variable in multithreaded environment nin234@yahoo.com C++ 1 02-23-2005 05:34 PM
Database & 'autonumber' in multithreaded environment Joerg Gippert Java 9 10-10-2003 08:13 PM



Advertisments