Re: std::ifstream with thread

Discussion in 'C++' started by Alf P. Steinbach /Usenet, Sep 9, 2010.

  1. * Philipp Kraus, on 09.09.2010 13:37:
    > Hi,
    >
    > I'm writing a multithreaded program, which must read some files. I create my
    > thread and run them. Each thead holds a list with filenames, but this list is
    > not unqiue for the thread, so more than one thread can be read the same files. I
    > open the file with std::istream std::ifstream::in | std::ifstream::binary and
    > use a std::copy to get the data, after that I close the file. If I analyse my
    > program with valgrind I'll get the message "recursive write lock granted on
    > mutex/wrlock which does not support recursion" for libpthread
    >
    > In my application I must read (only) a lot of files and work with the content,
    > so I would do this with thread. But it can be, that more than one thread reads
    > the file at the same time.
    > How can I solve the problem with the recusive lock?


    I suggest first trying to reproduce the problem without treading, in a small
    program.

    If cannot be reproduced that way, extend with threading (just 2 threads).

    That is, REPRODUCE the problem in a minimalistic setting.

    When you know what causes it it's easier to device a solution.

    And if you manage to reproduce but no cause is apparent or no solution is
    apparent, post the complete minimalistic program that reproduces problem.


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Sep 9, 2010
    #1
    1. Advertising

  2. On 2010-09-09 17:23:58 +0200, Alf P. Steinbach /Usenet said:

    > * Philipp Kraus, on 09.09.2010 13:37:
    >> Hi,
    >>
    >> I'm writing a multithreaded program, which must read some files. I create my
    >> thread and run them. Each thead holds a list with filenames, but this list is
    >> not unqiue for the thread, so more than one thread can be read the same
    >> files. I
    >> open the file with std::istream std::ifstream::in | std::ifstream::binary and
    >> use a std::copy to get the data, after that I close the file. If I analyse my
    >> program with valgrind I'll get the message "recursive write lock granted on
    >> mutex/wrlock which does not support recursion" for libpthread
    >>
    >> In my application I must read (only) a lot of files and work with the content,
    >> so I would do this with thread. But it can be, that more than one thread reads
    >> the file at the same time.
    >> How can I solve the problem with the recusive lock?

    >
    > I suggest first trying to reproduce the problem without treading, in a
    > small program.
    >
    > If cannot be reproduced that way, extend with threading (just 2 threads).
    >
    > That is, REPRODUCE the problem in a minimalistic setting.


    The main problem is that I use not the native pthreads. I use boost
    threads. I have switched the std::streams to the boost streams and that
    helps a little bit.
    I found out, that only one line creates an error

    std::copy( std::istream_iterator<char>(l_file),
    std::istream_iterator<char>(),
    std::eek:streambuf_iterator<char>(&l_deflate) );

    This linke creates the problem, but I don't understand it. I would like
    to copy the whole data from l_file into l_deflate (both are boost
    stream objects). Boost has got a own copy method, but I can't use them,
    because after copy it closes both streams. I would like to copy the
    data without closing both streams.

    The variables are
    bio::stream< bio::basic_file_source<char> > l_file
    bio::filtering_streambuf< bio::eek:utput >

    bio is a boost::iostream namespace.

    I hope that anyone has got any idea or a tip.
    Thanks

    Phil
     
    Philipp Kraus, Sep 13, 2010
    #2
    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. Peter Jansson
    Replies:
    5
    Views:
    6,330
    Ivan Vecerina
    Mar 17, 2005
  2. Vinu
    Replies:
    4
    Views:
    368
    Jim Langston
    Jul 7, 2005
  3. Assertor
    Replies:
    5
    Views:
    392
  4. Howard Hinnant

    Re: std::ifstream with thread

    Howard Hinnant, Sep 9, 2010, in forum: C++
    Replies:
    2
    Views:
    608
    Howard Hinnant
    Sep 10, 2010
  5. mathieu
    Replies:
    12
    Views:
    1,247
    Jeff Flinn
    May 25, 2011
Loading...

Share This Page