c++ - Multiple threads reading from the same file -
My forum is Windows Vista 32, with Visual C ++ Express 2008.
For example:
If I have a file that has 4000 bytes, can I read 4 threads from the file at the same time? And each thread reaches a different section of the file.
read thread 1, 0-999, thread 2 read 1000 - 2999, etc.
Please give an example in C language.
If you do not write them, there is no need to keep in mind the status of the sync / running is.
Just open the file with sharing as a separate handle and everything will work (i.e., you have to open the file in the context of the thread rather than handle the same file).
#include & lt; Stdio.h & gt; #include & lt; Windows.h & gt; DWORD WINAPI Mithred (LPVOD Ultimate) {int i = (int) the ultimate; Byte Buff [1000]; DWORD number; Handle H = Make File ("c: \\ test.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); SetFilePointer (h, i * 1000, NULL, FILE_BEGIN); Readfile (h, buff, size (buff), and amp; numread, NULL); Printf ("buf [% d]:% 02X% 02X% 02X \ n", I + 1, buff [0], buff [1], buff [2]); Return 0; } Int main () {int i; Handle H [4]; For (i = 0; i <4; i ++) h [i] = skin and ferred (zero, 0, mithred, (LPVOD), 0, NULL); // (i = 0; i & lt; 4; i ++) WaitfosarSynic Object (H [i], INFINITE); WaitingFilmtube Objects (4, H, Truth, INFINITE); Return 0; }
Comments
Post a Comment