Re: Two threads reading from same file?
- From: "Hector Santos" <nospamhere@xxxxxxxxxxxxxx>
- Date: Wed, 13 Apr 2005 12:45:31 -0400
"noleander" <noleander@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:680A58CB-46FD-4DDA-9703-0F56E7762CBC@xxxxxxxxxxxxxxxx
> You are correct: To test that the threads were reading the
> proper part of the disk file, I filled the disk file with 4-byte
> integers 0,1,2,3,.... When reading from disk index 999
> (at byte 4*999) the integer read should be 999.
Ok, then the verification logic shown in the code below
should be sufficient to prove it works fine. :-)
------------------ start: cut and paste ---------
// File: TestShareFile.cpp
//
// complile: CL testsharefile.cpp /W3 /GX /MD /D "_AFXDLL"
//
// usage:
//
// To create the test file, type:
//
// testsharefile /c
//
// To test the file reading with 20 multiple threads
//
// testsharefile
#include "stdafx.h"
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define NUM_THREADS 20
#define TEST_FILE_NAME "testsharefile.dat"
#define TOTAL_RECORDS 10000
void WINAPI PrintHello(DWORD)
{
DWORD tid = GetCurrentThreadId();
FILE* fv = fopen(TEST_FILE_NAME,"rb");
if (!fv) {
printf("%04d: Error %d fopen()\n",tid,GetLastError());
return;
}
int loop = 1000;
int amt = 10000;
int size = sizeof(int); // record size
int *pBuffer = (int *)malloc(amt*size);
printf("%04d: reading\n",tid);
for ( int i=0; i < loop; i ++ ) {
//-------------------------------------------------------
// Dance Baby! Dance!
//-------------------------------------------------------
int index = (rand() % loop)*size;
if (fseek ( fv, index, SEEK_SET )) {
printf("%04d: Error %d fseek()\n",tid,GetLastError());
break;
}
//-------------------------------------------------------
// read records (vector of integers)
//-------------------------------------------------------
int bpos = ftell(fv); // verification base index
int r = fread (pBuffer, size, amt, fv);
if (r != amt) {
// Remove this to increase verification
//printf("%04d: error fread() r = %d\n",tid,r);
//break;
}
//-------------------------------------------------------
// Verify buffer. Vector of integers correspond to the
// the file index.
//-------------------------------------------------------
for (int n=0; n < r; n++) {
int expected = (bpos+n*size)/size;
if (expected != pBuffer[n]) {
// Logic error!
printf("! %4d: bpos: %6d n: %4d buf[n]: %4d exp: %5d\n",
tid,bpos,n,pBuffer[n],expected);
break;
}
}
}
fclose(fv);
free(pBuffer);
printf("%04d: finish\n",tid);
return;
}
void CreateTestFile()
{
printf("- creating test file %s\n",TEST_FILE_NAME);
FILE* fv = fopen(TEST_FILE_NAME,"wb");
if (!fv) {
printf("Error %d CreateTestFile() fopen() \n",GetLastError());
return;
}
for (int i = 0; i < TOTAL_RECORDS; i++) {
if (!fwrite(&i,sizeof(i),1,fv)) {
printf("Error %d fwrite\n",GetLastError());
break;
}
}
fclose(fv);
}
int _tmain(int argc, _TCHAR* argv[])
{
if (argc > 1 && !stricmp(argv[1],"/c")) {
CreateTestFile();
return 0;
}
HANDLE hThreads[NUM_THREADS];
DWORD tid;
int t;
printf("* Starting threads\n");
for(t=0;t < NUM_THREADS;t++){
printf("- Creating thread %d\n", t);
hThreads[t] = CreateThread(
NULL,
0,
(LPTHREAD_START_ROUTINE) PrintHello,
0,
CREATE_SUSPENDED,
&tid);
}
printf("* Resuming threads\n");
for(t=0;t < NUM_THREADS;t++) ResumeThread(hThreads[t]);
while (WaitForMultipleObjects(NUM_THREADS,
hThreads, TRUE, 100) == WAIT_TIMEOUT) {
if (kbhit() && getch() == 27) {
break;
}
}
printf("* Done\n");
return 0;
}
------------------ end: cut and paste ---------
--
Hector Santos, Santronics Software, Inc.
http://www.santronics.com
.
- References:
- Two threads reading from same file?
- From: noleander
- Re: Two threads reading from same file?
- From: Slava M. Usov
- Re: Two threads reading from same file?
- From: noleander
- Re: Two threads reading from same file?
- From: Hector Santos
- Re: Two threads reading from same file?
- From: Hector Santos
- Re: Two threads reading from same file?
- From: noleander
- Re: Two threads reading from same file?
- From: Hector Santos
- Re: Two threads reading from same file?
- From: Hector Santos
- Re: Two threads reading from same file?
- From: noleander
- Re: Two threads reading from same file?
- From: Hector Santos
- Re: Two threads reading from same file?
- From: noleander
- Two threads reading from same file?
- Prev by Date: Re: HANDLE Thread Safety
- Next by Date: Re: HANDLE Thread Safety
- Previous by thread: Re: Two threads reading from same file?
- Next by thread: Bluetooth Driver Signing
- Index(es):
Relevant Pages
|