views:

94

answers:

3

This is an offshoot of this question but with a few constraints removed.

I have a system where I need to manage file locking. I need to be able to lock a file (shared read locking) in one thread and then unlock it in another. More accurately, I can't be sure what thread it will be unlocked in or even if the creating thread is still around.

I will also need exclusive write locking to go with this but that will be all in the same thread.

the .NET Mutex won't work as it does extra stuff when the creating thread exits

+1  A: 

Maybe a (named) Semaphore with a count of one? WaitOne to lock, Release to unlock?

Marc Gravell
see edit: forgot to mention I'm looking for shared locks. Aside from that +1
BCS
A: 

Sounds to me like a Turnstile problem. You need to manage the readers going in with the first one locking the file and the last one to leave unlocking the file. You can use a semaphore to implement this. You can give preference to the writer by having the turnstile deny readers while there is a writer waiting.

Stinomus
That's exactly what I want, but I haven't found a way to do it where the locking and unlocking can happen in different threads (Thread A locks, thread B unlocks)
BCS
You could have a custom semaphore that locks the file when the first token is taken and then unlocks the file when the last token is returned.
Stinomus
Just to clarify, the read lock is then maintained by the semaphore so it is not actually held by a thread as such. The first thread effectively opens the gate and then the last thread closes it this is managed by the semaphore when the token count is incremented from 0 or decremented to 0, the threads themselves do not know whether they are the ones who are opening or closing the gate.
Stinomus
+1  A: 

The question that you linked to is crossing process boundaries, but from what I read you are only crossing threads... with this in mind I think that Jeff Richter's ReaderWriterGate class might fit your problem well. It allows you to control access to a shared resource and queue access requests to the resource. It doesn't seem to have any thread affinity so if you are not crossing process boundaries you it might be a solution for you.

Here is a link to an article about the class... Concurrent affairs and you can download the PowerThreading Library from here

If your case is really simple (except for the locking from one thread and releasing from another) I don't see why you couldn't use the built in ReaderWriterLock in .NET (although the one in the PowerThreading library is supposed to be much faster). The Monitor class has no thread affinity and can be accessed from any context so depending on how you are using it this may be your most straight forward solution.

Brian ONeil
I'll take a look at them. The MSDN links sound more like what I really want.
BCS
Gerrr... ReaderWriterLock, ReaderWriterLockSlim and Monitor are all thread locked. :(
BCS
Did you look at Richter's stuff?
Brian ONeil
I'm not really in a position to drag in external dependencies.
BCS