15

I'm late to the party, but I recently learned about SemaphoreSlim:

I used to use lock for synchronous locking, and a busy boolean for asynchronous locking. Now I just use SemaphoreSlim for everything.

private SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);

private void DoStuff()
{
    semaphoreSlim.Wait();
    try
    {
        DoBlockingStuff();
    }
    finally
    {
        semaphoreSlim.Release();
    }
}

vs

private object locker = new object();

private void DoStuff()
{
    lock(locker)
    {
        DoBlockingStuff();
    }
}

Are there any synchronous cases where I should prefer using lock over SemaphoreSlim? If so, what are they?

0

2 Answers 2

26

Here are the advantages of the lock over the SemaphoreSlim:

  1. The lock is reentrant, while the SemaphoreSlim is not. So programming with the lock is more forgiving. In case there is a rare path in your app where you are acquiring the same lock twice, the lock will acquire it successfully, while the SemaphoreSlim will deadlock.

  2. The lock is syntactic sugar around the Monitor class. In other words there is language support for the Monitor in C#, and there isn't for the SemaphoreSlim. So using the lock is comparatively more convenient and less verbose.

  3. You can write more robust code with the lock, because you can add debugging assertions in auxiliary methods that the lock has been acquired: Debug.Assert(Monitor.IsEntered(_locker));

  4. You can get contention statistics with the Monitor.LockContentionCount property: "Gets the number of times there was contention when trying to take the monitor's lock." There are no statistics available for the SemaphoreSlim class.

  5. The SemaphoreSlim is IDisposable, so you have to think about when (and whether) to dispose it. Can you get away without disposing it? Are you disposing it prematurely and risk an ObjectDisposedException? These are questions that you don't have to answer with the lock.

  6. The lock can survive in the scenario of an aborted thread. It is translated by the C# compiler like this:

    bool lockTaken = false;
    try
    {
        Monitor.Enter(obj, ref lockTaken);
        /* Here goes the code inside the `lock`. */
    }
    finally
    {
        if (lockTaken)
        {
            Monitor.Exit(obj);
        }
    }

The Monitor.Enter has been coded carefully so that in case the thread is aborted, the lockTaken will have the correct value. On the contrary the SemaphoreSlim.Wait is typically called outside of the try/finally block, so there is a small window that the current thread can be aborted without releasing the lock, resulting in a deadlock.

The .NET Core/5+ platform has dropped support for the Thread.Abort method, so you could rightfully say that the last point has only theoretical value.

7

I know this is about advantages and not disadvantages, but a lock disadvantage is that it does not work well with async await. That is because the thread might be changed between before and after the await. But you are not allowed to enter the lock on one thread and leave it on another. When this happens the lock will throw an exception. SemaphoreSlim does not have this issue. https://stackoverflow.com/a/75781414

When using lock directly with await, the compiler will throw an error when you try to build it. So you should not run into this issue. But when you use Monitor.Enter/Exit instead of lock (lock is syntactic suger for Monitor.Enter/Exit), the compiler will not notice the problem and you may run into exceptions at runtime. https://stackoverflow.com/a/21404261

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.