C++ and Win32 Multithread FileRead

I am trying to write a multithread file reader.. but i cant get what i want yield. Do you know why ??
(Sorry for my english :)

this is my thread function ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
FILE *f ;
 DWORD WINAPI thread_fonksiyonu(LPVOID lpVoid) 
{	
	int i = *((int *) lpVoid);
	printf(" %d\n",i);
	char s[8192];
	while( (fread(&s,8192,1,f)) != 0) 
	{
		printf("%s",s);
	}
	satir_sayisi++;
	return 0;
}
// file pointer's initialized in WinMain function
 

Where is my wrong ?
WinMain is initialize file pointer. WinMain is creating threads with getting a number from user. That is it. When i use single thread, the program finished 260second. When i use multithread 284second !? What should i do ?
There are a few things wrong with this. I recommend that you read a tutorial on concurrency.

1: No synchronization
Each of your threads modifies the same value, but you can't guarantee the order in which your code runs. What will happen when *f is changed by a different thread in the middle of another's fread() call? You're not locking access to *f or anything else shared among threads so you're suffering from race conditions.

2: Reading the same file most likely can't be parallelized effectively
See http://stackoverflow.com/a/10625178

3: Each of your threads attempts to read the entire file
Concurrency is fundamentally a performance hack. The idea is to distribute work among multiple "workers", so that a job gets done faster. However, not every task is suited to having more workers thrown at it.

You are trying to read the entire file in every thread. Barring the hardware issue which makes this all pointless, a better idea would be to have each thread read a different, equally-sized part of the file, storing it in the appropriate place in a single buffer.

The main program would start each thread and then wait for all of the threads to complete their task (perhaps in parallel), and then use the buffer once it was finished being modified. If you tried this, you should have each thread open the file, so that you don't need to wait for all the other threads to be finished with the same file handle before you take over.

If you are writing C++ code (it doesn't look like it), then it would be a good idea to use the standard C++11 <thread> library if it is available to you.
thank you. i included thread header file and i used it. its very simple and easy to use. is thread header file portable, isnt it?
<thread> is part of the standard template library (STL).

It is portable.
<thread> is part of the standard template library (STL).

It is portable.


No, it's not. It's part of the C++ Standard Library.
Topic archived. No new replies allowed.