fread reads wrong data in Multithreaded app.

Hi,
My application continuously reads and then writes the same data into a .txt file.I use fread and fseek for reading. But when there are around 7-8 threads doing the read and write the file gets corrupted.This doesn't happen always only sometimes.
What I could see in from my logs is fread some times read some random value,and sometimes fseek doesn't jump properly.
What I understand is fread, fseek methods are threadsafe and even my syncronizing mechanism works.I use WaitForSingleObject api with Mutex handle.
Any Idea on why fread and fseek donot work as expected neither do they fail so that I can catch the failure.


The code snippet is below:
.....
Lock(gMutexHandle[ifp]);

fseek (pTemp->fpBlob, 4, SEEK_SET);
match = FALSE;
while (!match)
{
if(ppBlob_File != NULL)
*ppBlob_File = pTemp;

pTemp->curPos = ftell (pTemp->fpBlob);
cnt = (LONG)fread (&tBlob, BLOB_HEADER_SIZE, 1, pTemp->fpBlob);
if (!cnt)
{ ...
}
if (pDataBlob->blobVersion != BLOB_VERSION_ANY)
{
if (pDataBlob->blobVersion != tBlob.blobVersion)
{
fseek (pTemp->fpBlob, tBlob.blobSize - BLOB_HEADER_SIZE, SEEK_CUR);
continue;
}
}
if (pDataBlob->blobUserId[0] != '\0')
{
if (strcmp (pDataBlob->blobUserId, tBlob.blobUserId))
{
fseek (pTemp->fpBlob, tBlob.blobSize - BLOB_HEADER_SIZE, SEEK_CUR);
continue;
}
}
..........
//
// it's a match.
//
match = TRUE;
}

UnLock(gMutexHandle[ifp]);

Any suggestions on this?

Thank You,
Pradeep
Topic archived. No new replies allowed.