Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to read the same file from tow different threads?, i tried to open the file at each thread and read its not working?
any ideas?
I am using <windows.h>
Posted
Updated 26-Oct-10 7:09am
v2
Comments
Richard MacCutchan 26-Oct-10 13:09pm    
Minor edit.
Richard MacCutchan 26-Oct-10 13:10pm    
What do you mean, you are using <windows.h>? This is a header file and has nothing to do with reading files.

If you lock the file in one thread, then the 2nd thread will not be able to open the file. This is most likely your problem (based on the limited info provided here).
 
Share this answer
 
Thanks, I know that this is the problem, the question is how to solve it ?


It depends on how you are doing it in your present code. If you want the two threads to independently open and read the file, make sure you open it in a read mode that's nonexclusive (meaning it won't lock the file from being opened for read access).

Alternatively, you can share the file handle and use that from both threads, though that may bring in side effects that you'd then need to work around. But it's doable.
 
Share this answer
 
You said that you are using the windows.h header; I suppose that you mean that you are using the Win32 API, without MFC, ATL nor other frameworks. Isn't it?

The Win32 way to open a file is to use the CreateFile Function (Windows)[^]; the third parameter of that function is dwShareMode, which tells Windows if you want to get an exclusive access to the file or not. If you specified zero for that parameter you have denied others to do anything on the file. To give others the ability to open the file for reading you should use FILE_SHARE_READ.

See the code snippet below:

C++
HANDLE hFile = CreateFile(
   _T("myfile.txt"),    // File name
   GENERIC_READ,        // Open for reading
   FILE_SHARE_READ,     // Allow others to read the file (but not modify it)
   NULL,                // Don't use security attributes
   OPEN_EXISTING,       // The file should already exist
   0,                   // We don't need flags and attributes
   NULL);               // Ignored while we are opening an existing file
 
Share this answer
 
v2
How you do this depends on why you want to process the file from two different threads. If you want the threads to just read the file sequentially and do something to it then you open the file for shared reading the way Sauro suggested.

However if you want the threads to have interleaved access to the file (e.g. it's record oriented and you only want each record processed once) then open the file once exclusively, pass the handle /stream/whatver to each thread on creation and synchronise access to the file. The same sort of thing applies to random access to the file - make sure that the entire position/read sequence is synchronised between your threads.

Finally, if you want to use random access to the file and have got enough address space then memory mapping the file simplifies the client code a lot, especially if the records are all the same size. Open the file, map it and then woo hoo you can use array indexing to access the correct record - no explicit reading reading. And you don't have to worry about synchronising access through the file handle.

Cheers,

Ash
 
Share this answer
 
Thanks, I know that this is the problem, the question is how to solve it ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900