Why does the ReaderWriterLock program not start automatically when the user shuts down?

This is a theoretical question; I don't have a real problem to solve, I just want to understand how this works.

I found out that if a thread does not allow you to exit ReaderWriterLock, then other threads will not be able to get a lock even after the end of the original thread. Is there a good reason why ReaderWriterLock does not provide a lock to waiting threads as soon as the thread to which the lock belongs ends?

Here is an example that demonstrates the problem.

    static void Main(string[] args)
    {
        ReaderWriterLockSlim readerWriterLock = new ReaderWriterLockSlim();

        Thread t1 = new Thread((a) =>
        {
            readerWriterLock.EnterReadLock();

            // this thread omits to Exit the lock....
        });

        Thread t2 = new Thread((a) =>
        {
            readerWriterLock.EnterWriteLock();
        });

        t1.Start();
        t2.Start();

        // wait for all threads to finish
        t1.Join();
        t2.Join();
    }
+3
source share
2 answers

: , , .

, . , , , , - - , , .

, , , , , .

+3

, , , , . , , , , , . , , , , , . , , , , , - , , , .

0

Source: https://habr.com/ru/post/1732236/


All Articles