Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

ReaderWriterLock class

Back to Forum: Tech Off
  • User profile image
    gaelhatchue

    I find that the MSDN documentation of this class is confusing.

    I need to know how to use RWL to lock multiple ressources.

    The example in the MSDN doc. uses a static instance of the class, to protect a static resource. And there seems to be no link between the RWL class and the resource itself, so I assume that a single instance of this class can be used to lock all resources in a single thread.

    I just took a look at the description of some public properties of this class:

      IsReaderLockHeld Gets a value indicating whether the current thread holds a reader lock.
      IsWriterLockHeld Gets a value indicating whether the current thread holds the writer lock.


    The description seem to indicate that RWL instances are somehow binded to a thread. So, if I instanciate 2 RWL classes, and aquire a reader lock with one instance, the property IsReaderLockHeld should return true on the second instance.

    This doesn't seem to be the case. (I verified this with a simple console app).

    So, I just don't know how to use this class to lock multiple resources.

    I have a class

    public class Foo
    {
       SomeRefType obj1;
       SomeOtherRefType ob2;
    }

    I have a few instances of this class, and multiple threads that can write/read the fields obj1 and obj2 in these instances at anytime.

    So, how implement the locking mechanism using the ReaderWriterLock class?

    Do I declare a static instance of RWL in my Foo class to lock all objects???

  • User profile image
    androidi

    Some notes about RWL that might affect you.


    http://wesnerm.blogs.com/net_undocumented/2005/04/the_art_of_thre.html

    ... Or then again could be irrelevant in your case.

  • User profile image
    gaelhatchue

    Thank you for the link, but it is indeed irrelevant to the ReaderWriterLock class in the .net framework.

    I have looked all over the net, but i can't find anything on this class. The poor sample code on MSDN is not helping either.

  • User profile image
    lorad

    ok if you want to read/write all the static instances as one single unit then you would only need one static RWL.

    If you have two items that need to be read/written independtly then you would need two static RWLs.

    The RWL is not bound to any thread, but a read or write lock is. The difference being that multiple threads can get read locks concurrently. As long there is at least one read lock you cannot get a write lock. Once you get a write lock you cannot get any read locks until the write lock is released.

    The IsXLockHeld is used to indicate if your thread has any lock on the object. This would be useful if your thread asks for a Read lock, but certain methods need Write locks, you don't want to ask for a Write lock when you already have a Read lock or you will deadlock yourself.

    public class Foo
    {
    static ReaderWriterLock rwl = new ReaderWriterLock();
    static int value1;
    static int value2;
    }

    you could use rwl to control access to value1 & value2 at the same time, or you could create rwl2 to control the access to value2, and use rwl to control the access to value1.

    The example might be a little confusing since it also has other static members that are incremented using Interlocked.Increment() which is a simple lock around incrementing a counter of some kind.



  • User profile image
    gaelhatchue

    Thanks for your help.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.