.Net Remoting And ReaderWriterLock

Hi,

I am working on a .Net Remoting program. In side the remoting class, I need to use a ReaderWriterLock object. But it seems that all method calls from remoting client can easily acquire the writer lock on that object regardless the lock has been acquired before.

Here is the code sample

public class RemotingExampleService : MarshalByRefObject
{
    private ReaderWriterLock lockObj = new ReaderWriterLock();
    public void  LockObject()
    {
        Console.WriteLine(lockObj.IsWriterLockHeld); //return true except the first call
        lockObj.AcquireWriterLock(-1);
    }
}

I used Singleton remoting mode, so there is only one remoting server object there.

Can anyone help

Thanks

 



Answer this question

.Net Remoting And ReaderWriterLock

  • LeahGarrett

    I tried Mutex instead of ReaderWriterLock, and I got the same result.
  • AdeptBlue

    Oh, it is not a problem if only I acquire and release the lock in the same method.


  • Batisse

    Yes, you are correct, I plan to reconstruct my program.

    But I think even if I control the lock only inside the remoting server object, I still can't make the ReaderWriterLock working well, because it is still inside that ThreadPool.

    Michael


  • BAK62

    From MSDN:

    AcquireWriterLock supports recursive writer-lock requests. That is, a thread can call AcquireWriterLock multiple times, which increments the lock count each time. You must call ReleaseWriterLock once for each time you call AcquireWriterLock. Alternatively, you can call ReleaseLock to reduce the lock count to zero immediately.

    Recursive lock requests are always granted immediately, without placing the requesting thread in the writer queue.

    You get the same thread because one is reused from the ThreadPool. More threads are needed only if there is enough load.

    Besides that, you might consider a different locking strategy. The way you're doing it now, you're asking for trouble. What if a client fails and doesn't call the method that releases the lock. A remote call should do as much work as possible and release all the resources, including locks.


  • anydobbo

    It seems that all remoting method calls result in a thread with the same ManagedThreadId,

    public void  LockObject()
    {
        Console.WriteLine(Thread.CurrentThread.ManagedThreadId); //It is always the same Id
        lockObj.AcquireWriterLock(-1);
    }

    Maybe this is the reason. If so, how can I make it into different thread


  • Rask

    I found some similiar question at

    http://www.dotnet247.com/247reference/msgs/56/281905.aspx

    But there is no answer there I think.


  • .Net Remoting And ReaderWriterLock