Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Downgrading ReaderWriterLockSlim UpgradeableReadLock to simple ReadLock

Downgrading ReaderWriterLockSlim UpgradeableReadLock to simple ReadLock

Scheduled Pinned Locked Moved C#
questioncom
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HosamAly
    wrote on last edited by
    #1

    The documentation of ReaderWriterLockSlim.EnterUpgradeableReadLock says: "A thread in upgradeable mode can downgrade to read mode or upgrade to write mode." How do I downgrade the lock to a read lock? The documentation does't tell...

    My LinkedIn Profile

    N 1 Reply Last reply
    0
    • H HosamAly

      The documentation of ReaderWriterLockSlim.EnterUpgradeableReadLock says: "A thread in upgradeable mode can downgrade to read mode or upgrade to write mode." How do I downgrade the lock to a read lock? The documentation does't tell...

      My LinkedIn Profile

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      To upgrade to write mode call EnterWriteLock and to downgrade, call ExitWriteLock

      Navaneeth How to use google | Ask smart questions

      H 1 Reply Last reply
      0
      • N N a v a n e e t h

        To upgrade to write mode call EnterWriteLock and to downgrade, call ExitWriteLock

        Navaneeth How to use google | Ask smart questions

        H Offline
        H Offline
        HosamAly
        wrote on last edited by
        #3

        Thanks, but I meant to downgrade from UpgradeableReadLock to simple ReadLock. I don't need WriteLock. The rationale is that since only one thread can acquire UpgradeableReadMode, I want to downgrade it when I know it's not needed so that another thread can acquire it.

        My LinkedIn Profile

        N 1 Reply Last reply
        0
        • H HosamAly

          Thanks, but I meant to downgrade from UpgradeableReadLock to simple ReadLock. I don't need WriteLock. The rationale is that since only one thread can acquire UpgradeableReadMode, I want to downgrade it when I know it's not needed so that another thread can acquire it.

          My LinkedIn Profile

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          Well, the below code will answer it

          class Program
          {
          static ReaderWriterLockSlim rwSlim = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
          static void Main(string[] args) {
          // entering upgradable read lock
          rwSlim.EnterUpgradeableReadLock();
          Console.WriteLine(rwSlim.IsUpgradeableReadLockHeld);

                  // downgrading to readlock. Now you are in read lock
                  rwSlim.EnterReadLock();
                  Console.WriteLine(rwSlim.IsReadLockHeld);
          
                  // exiting from upgradable lock
                  rwSlim.ExitUpgradeableReadLock();
                  Console.WriteLine(rwSlim.IsUpgradeableReadLockHeld);
          
                  // exiting read lock
                  rwSlim.ExitReadLock();
                  Console.WriteLine(rwSlim.IsReadLockHeld);
              }
          

          }

          You need to supply LockRecursionPolicy.SupportsRecursion to indicate that you need recursive locks.

          Navaneeth How to use google | Ask smart questions

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups