Is ReaderWriterLockSlim the right choice?

I am writing a global error handler / logger for applications running on Windows Azure. When an error occurs in the application, a series of operations are performed that must be performed atomically. I need the error not to be recorded until the previous one is completed. Meanwhile, I want to read magazines as needed.

My initial thought was to use a monitor / lock and lock only error recording. Thus, reading is not canceled at all. I was wondering if ReaderWriterLockSlim could be more suitable. I cannot say that I really understand the value between one approach and another.

Should I create a ReaderWriterLockSlim and do something like the following below (with reads that will be wrapped in EnterReadLock) ...

public static void LogError(Exception exception)
{
    _lock.EnterWriteLock();

    ...

    _lock.ExitWriteLock();
}

Or am I just doing something like the following, blocking only the records:

public static void LogError(Exception exception)
{
     lock (someStaticLock)
     {
        ...
     }
}

Any thoughts / advice would be highly appreciated.

+3
source share
3 answers

OK, it totally depends on how resource competition is expected. The following is a simple solution that I would make based on what I am blocking and how much blocking.

ReaderWriterLockSlim is implemented using spin-lock, so if you have long blocking resources (in this case, writing text), this will lead to performance degradation due to rotation on waiting threads. However, it is a very useful tool in the following situations.

  • , ( ), ReaderWriterLockSlim (spinlock).
  • - - , .

, , , .

ReaderWriterLockSlim , ReaderWriterLock, 3-5 .

+4

? enqueue/dequeue . , , , . , - , , , .

+3

, , , , . ReaderWriterLock , , , .

-1

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


All Articles