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: 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. ... > around the fread() call ... ... > put mutex locks around the fseek() and freadcalls. ...
    (microsoft.public.win32.programmer.kernel)
  • Reading jpeg binaries from mysql-db
    ... meanwhile I can store jpeg's and bitmap's data in mysql longblob fiedltype. ... Reading from this field over blobstream does work correctly. ... This error comes directly after the open statement. ...
    (borland.public.delphi.database.ado)
  • Re: Record Deliniating
    ... PDirected Transfers ... I have the open statement as: ... Open FileName For Input As #1 ... When reading the file, ...
    (microsoft.public.access.formscoding)
  • Record Deliniating
    ... I have a sequential file that contains records ... I have the open statement as: ... Open FileName For Input As #1 ... When reading the file, ...
    (microsoft.public.access.formscoding)