Re: Two threads reading from same file?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Each thread needs to open its own stream, Move the open statement to the
thread. This is ok if you are just opening and reading the file. Otherwise,
you need to use synchronization techniques.

If you must share the same file handle, then you MUST use synchronization
techniques. This is not normal. It is doesn't not defeat the purpose.

--
Hector Santos, Santronics Software, Inc.
http://www.santronics.com



"noleander" <noleander@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:20F20937-9B6D-43D9-99E9-1B35163E7DC3@xxxxxxxxxxxxxxxx
> Here is the code, with just 2 threads. Note that I am not using mutex
locks
> around the fread() call ... in other words, I am assuming that fread()
and
> fseek() are thread-safe. In the code below the "ERROR" message gets
printed
> out sometimes (sporadically) which means that fseek() and fread() are
> behaving in a non-thread-safe manner. The "ERROR" message goes away if I
> put mutex locks around the fseek() and fread() calls. But that would
defeat
> the point of having threads (that is, I may as well do all the file IO in
one
> thread, if fseek() and fread() require locks).
>
> -------- BEGIN CODE ------------------
>
> #include "stdafx.h"
> #include <pthread.h>
> #include <io.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <fcntl.h> // used for _O_BINARY etc.
> #include <sys/types.h>
> #include <sys/stat.h>
>
> #define NUM_THREADS 2
>
> int file_id_num;
>
> void *PrintHello(void *threadid)
> {
> FILE* file = _fdopen ( file_id_num, "rbR" );
>
> int buffer[20000];
>
> for ( int i=0; i<100; i ++ ) {
> _sleep ( rand() % 100 );
> fseek ( file, i*4, SEEK_SET ); // advance 4 bytes thru file each
loop
> fread ( &(buffer), 4, 1000, file ); // read 4,000 bytes
> // contents of file are 4-byte words with increasing values:
> 0,1,2,3,...
> if ( buffer[0] != i ) {
> // read completed, but read from the wrong part of the disk
file
> printf ( " ****** ERROR at %d-%d-%d \n", threadid,
buffer[0],
> i );
> }
> }
> pthread_exit(NULL);
> return NULL;
> }
>
>
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> file_id_num = _open (
> "C:\\JUNK3\\junk",
> O_RDONLY|O_BINARY|_O_SEQUENTIAL|_O_NOINHERIT );
>
> pthread_t threads[NUM_THREADS];
> int rc, t;
> for(t=0;t < NUM_THREADS;t++){
> printf("Creating thread %d\n", t);
> rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
> if (rc){
> printf("ERROR; return code from pthread_create() is %d\n",
rc);
> exit(-1);
> }
> }
> return 0;
> }
>
> -------------- END CODE ---------------------------------------
>
> "Slava M. Usov" wrote:
>
> > "noleander" <noleander@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> > news:5AA1F613-3F14-4338-B82D-02E0D19F49FE@xxxxxxxxxxxxxxxx
> > > I've got a Visual C++ application. It has two threads. Each thread
> > > needs to read from the same data file (no writing is necessary).
> > >
> > > No matter what I try, it appears that the two threads interfere with
each
> > > other's seek pointer. The reads succeed, but they are reading from
the
> > > wrong place in the file. Clearly, the seek pointers are getting
altered
> > > by the other thread.
> >
> > I suggest that you create a bare-bones version of the problematic code,
> > which is two threads reading from the same file, and, if it still has
the
> > problem, post it here.
> >
> > S
> >
> >
> >

.



Relevant Pages

  • Re: C fread stops at page boundary with EFAULT, but continues with single bytes
    ... I encountered a a truly strange problem in my up2date Centos with kernel ... the C function 'fread' would reproducibly ... As a workaround, I could simply continue reading from there, single bytes at a time. ... Why the single-byte fread's might work -- speculation: when you initially try to read a chunk that is bigger than a page (which is probably matched by the size of the stream's user-space buffer), the freadimplementation might try to issue a readsyscall that would directly transfer data from the file to the buffer you specified. ...
    (comp.os.linux.development.apps)
  • C fread stops at page boundary with EFAULT, but continues with single bytes
    ... When reading a certain file, the C function 'fread' would reproducibly ... int dsc_readbytes ... RETURNS EFAULT ...
    (comp.os.linux.development.apps)
  • Re: Two threads reading from same file?
    ... Each thread needs to open its own stream, Move the open statement to the ... This is ok if you are just opening and reading the file. ... int buffer; ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Monitoring a pthread with another pthread
    ... dvd that burned badly. ... across a run of bad data, fread spends a lot of time on each bad 2-byte ... DVDs use 2048-byte sectors, so reading less is not going to help. ...
    (comp.os.linux.development.apps)
  • fread 1 byte x N vs N bytes x 1
    ... When reading a binary input stream with fread() one can ... I would assume the latter form would be faster, ... the input is a multiple of the block size. ...
    (comp.lang.c)