Threadless Mutex
This is a fork of this question, but with a few restrictions removed.
I have a system where I need to manage file locking. I need to lock a file (read shared) on one thread and then unlock it on another. More precisely, I cannot be sure which thread it will unblock or even if the creating thread still exists.
I will also need an exclusive write lock for this, but everything will be on the same thread.
.NET Mutex will not work as it adds extra things when the creating thread exits
a source to share
The question you linked to is process boundary crossing, but from what I read, you are only thread crossing ... with that in mind, I think Jeff Richter's ReaderWriterGate class might be a good fit for your problem. This allows you to control access to a resource and access to shared resources. It seems to have no thread binding, so if you are not crossing process boundaries this might be the solution for you.
Here is a link to an article about the class ... Parallel questions and you can download the PowerThreading Library here
If your case is really simple (other than blocking from one thread and freeing from another), I don't understand why you couldn't use the built-in ReaderWriterLock in .NET (although one in the PowerThreading library should be much faster). the monitor class has no thread affinity and can be accessed from any context, so depending on how you use it this might be your most direct solution.
a source to share
Sounds like a turnstile problem to me. You need to manage your readers using the first one to lock the file and the last one to keep the file unlocked. You can use a semaphore to implement it. You can give preference to the writer if the turnstile rejects readers while they wait for the writer.
a source to share