iostreams in multithreaded environment

Discussion in 'C++' started by Anthony, Oct 8, 2003.

  1. Anthony

    Anthony Guest

    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
    Anthony, Oct 8, 2003
    #1
    1. Advertising

  2. "Anthony" <-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
    Victor Bazarov, Oct 8, 2003
    #2
    1. Advertising

  3. Anthony

    tom_usenet Guest

    On Wed, 8 Oct 2003 14:45:32 +0200, "Anthony"
    <-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
    tom_usenet, Oct 8, 2003
    #3
  4. > 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.
    Javier Estrada, Oct 8, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joerg Gippert
    Replies:
    9
    Views:
    666
    John C. Bollinger
    Oct 10, 2003
  2. Replies:
    1
    Views:
    689
    E. Robert Tisdale
    Feb 23, 2005
  3. Replies:
    9
    Views:
    430
    Pete Becker
    Mar 6, 2006
  4. Sheth Raxit
    Replies:
    1
    Views:
    482
    Richard Heathfield
    Nov 23, 2007
  5. Me Me
    Replies:
    5
    Views:
    105
    Me Me
    Jun 30, 2008
Loading...

Share This Page