Hi, I have two tasks 'Read' and 'Write' which reads and writes on a file "abc.txt" respectively.
Now I need to restrict the Write operation on the file while Read is going on, But can allow two Reads at a time.
ie. two Reads can happen simultaneously, but Write can't happen at Read is going on.
I can do this by using Mutex lock which can lock the file at the start of Read and release after completion of Read, But by doing this the second Read will also won't get access to the file.
I dont have much experience in this but may be you can use two locks ,
1) Read Lock. I am not sure but there is somthing as recursive lock which you can lock multiple times. It maintains a type of counter which increments everytime you lock it and decrements everytime you unlock it. If the value is say zero then read lock is unlocked and if > 0 then it is locked.
2) Write Lock
so,
At the start of Read the check fo the write lock, if it is not locked, read lock the file.
At the start of Write check for both Read and write lock and if both are free then write lock the file and start writing.