Stringbuf for concurrent writing And reading

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fuenfzig
    New Member
    • Nov 2006
    • 5

    Stringbuf for concurrent writing And reading

    Hi all,

    I want to use a single std::stringbuf for writing (by a std::ostream)
    and for reading (by a std::istream), concurrently in two threads.
    This came to my mind, because the code for reading should be as with any other istream and cannot be changed.

    Now my problem is, that a do not get any data in the reading loop
    std::ofstream tempfile("test. pdf",
    std::ios_base:: out | std::ios_base:: trunc | std::ios_base:: binary);
    while (!is->eof()) {
    is->read(buffer, bufferSize);
    tempfile.write( buffer, is->gcount());
    tempfile.flush;
    }

    The writing thread proceeds as expected and finally finishes writing.

    Any hints? What are my misconceptions?

    Thanks,
    Christoph
  • smartway
    New Member
    • Oct 2006
    • 24

    #2
    How did you define 'is'?

    Comment

    • fuenfzig
      New Member
      • Nov 2006
      • 5

      #3
      Originally posted by smartway
      How did you define 'is'?
      is is an istream* managed by a boost::shared_p tr
      Code:
      typedef boost::shared_ptr<std::istream> IStreamP;
      IStreamP is = data->getIStream();
      The istream is created with the same std::stringbuf used during writing (in a ostream of course!).
      It seems that I do not get any content during reading or the read pointer is always at the end or ..

      Comment

      • fuenfzig
        New Member
        • Nov 2006
        • 5

        #4
        Hi again,

        let me put it differently. I just tested, that it does not depend on
        multi-threading.
        If I write to the stringbuf in a std:: ostream (or a std::iostream) and
        then try to read
        with a std::istream* is (or the same std::iostream* is), it does not
        work either.
        I immediatly get, (is->fail() == true) :-(
        Also tried is->seekg(0); with the same result.

        Does anybody have an example showing usage of stringbuf with
        std:: ostream and std::istream?
        Or can somebody give some details on the internals, when
        (is->fail()==true ) and (is->eof()==true) does occur?

        Thanks very much,
        Christoph

        Comment

        Working...