Re: Two threads reading from same file?
- From: "Hector Santos" <nospamhere@xxxxxxxxxxxxxx>
- Date: Wed, 14 Jun 2006 04:12:56 GMT
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 mutexlocks
around the fread() call ... in other words, I am assuming that fread()and
fseek() are thread-safe. In the code below the "ERROR" message getsprinted
out sometimes (sporadically) which means that fseek() and fread() aredefeat
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
the point of having threads (that is, I may as well do all the file IO inone
thread, if fseek() and fread() require locks).loop
-------- 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
fread ( &(buffer), 4, 1000, file ); // read 4,000 bytesfile
// 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
printf ( " ****** ERROR at %d-%d-%d \n", threadid,buffer[0],
i );rc);
}
}
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",
exit(-1);each
}
}
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
theother's seek pointer. The reads succeed, but they are reading from
alteredwrong place in the file. Clearly, the seek pointers are getting
theby 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
problem, post it here.
S
.
- References:
- Re: Two threads reading from same file?
- From: Slava M. Usov
- Re: Two threads reading from same file?
- Prev by Date: Re: Two threads reading from same file?
- Next by Date: RE: win application and ctfmon conflict
- Previous by thread: Re: Two threads reading from same file?
- Next by thread: Re: Two threads reading from same file?
- Index(es):
Relevant Pages
|