Read/Write synchronizer , get the owner of the lock

85 views Asked by At

Probably this is a simple question, but due to lack of some knowledge on C# I'm having a hard time finding a solution for this.

Here's the deal, I have to implement a semaphore with 5 methods, lockRead, lockWrite, releaseRead, releaseWrite and releaseLockWriter.

The names of the first four methods are quite self-explanatory, the last one is a method that releases a writer lock and acquires read lock atomically.

Now my problem here is that releaseRead, releaseWrite and releaseLockWriter can only be called by a thread that has acquired the read or write lock, respectively.

How can I know if the thread is the owner of the lock? A possible solution would be creating id's for each thread and passing them when acquiring lock and then when releasing passing the id again and check if the id is owner, but probably there's a better way of doing it.

0

There are 0 answers