4

Reading on locks in C#. I see that being able to acquire lock on same object multiple times is said to be possible because Monitors are re-entrant. The definition of re-entrant code as defined in wikipedia does not seem to fit well in this context. Can you please help me understand what is re-entrancy in the context of C# and how does it apply to Monitors? From what I have understood, when a thread has acquired a lock, it would not relinquish the lock when in the middle of a critical section - even if it yields CPU..as a result of which, no other thread would be able to acquire the monitor..where does re-entrancy come into picture?

Aadith Ramia
  • 10,005
  • 19
  • 67
  • 86
  • Re your edit: re-entrancy comes into picture when the thread which is *already* holding the lock, calls `lock` or `Monitor.Enter` *again* on the same object. You might think that this would deadlock, but it succeeds and simply implements the internal counter. The same thread then has to call `Monitor.Exit` (or exit the `lock` scope) multiple times in order to truly release the lock. – Kirill Shlenskiy Jan 06 '16 at 07:23
  • wikipedia says - In computing, a computer program or subroutine is called reentrant if it can be interrupted in the middle of its execution and then safely called again...in the scenario in question, there is no interruption..isn't it? – Aadith Ramia Jan 06 '16 at 07:37
  • 2
    No, there is no interruption to speak of when it comes to lock reentrancy. You're mixing up two different concepts: method reentrancy (discussed in that Wikipedia article you linked) and lock reentrancy (see https://en.wikipedia.org/wiki/Reentrant_mutex). Any time you hear the word "reentrancy" applied to `lock`, `Monitor`, `Mutex` or any other synchronization primitive, it generally refers to the ability of the thread *already holding the lock* to acquire said lock *again* (you can think of it as lock recursion). – Kirill Shlenskiy Jan 06 '16 at 07:47

2 Answers2

6

@Zbynek Vyskovsky - kvr000 has already explained what reentrancy means with regards to Monitor.

Wikipedia defines "reentrant mutex" (recursive lock) as:

particular type of mutual exclusion (mutex) device that may be locked multiple times by the same process/thread, without causing a deadlock.

Here's a little example to help you visualise the concept:

void MonitorReentrancy()
{
    var obj = new object();

    lock (obj)
    {
        // Lock obtained. Must exit once to release.
        // No *other* thread can obtain a lock on obj
        // until this (outermost) "lock" scope completes.

        lock (obj) // Deadlock?
        {
            // Nope, we've just *re-entered* the lock.
            // Must exit twice to release.

            bool lockTaken = false;

            try
            {
                Monitor.Enter(obj, ref lockTaken); // Deadlock?

                // Nope, we've *re-entered* lock again.
                // Must exit three times to release.
            }
            finally
            {
                if (lockTaken) {
                    Monitor.Exit(obj);
                }

                // Must exit twice to release.
            }
        }

        // Must exit once to release.
    }

    // At this point we have finally truly released
    // the lock allowing other threads to obtain it.
}
Kirill Shlenskiy
  • 9,367
  • 27
  • 39
3

Reentrancy has many meanings actually.

Here in this context it means that the monitor can be entered by the same thread repeatedly several times and will unlock it once the same number of releases are done.

Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
  • Thanks. What else can re-entrancy mean? – Aadith Ramia Jan 06 '16 at 06:47
  • 1
    For example that the function can be called by multiple thread simultaneously (the problems with some original POSIX APIs - some of the functions didn't allow it - `localtime()` function). The other meaning is that the function is interruptible and can be re-entered again without any side effects. – Zbynek Vyskovsky - kvr000 Jan 06 '16 at 06:49