开发者

Explain the code: c# locking feature and threads

开发者 https://www.devze.com 2022-12-28 11:38 出处:网络
I used this pattern in a few projects, (this snipped of code is from CodeCampServer), I understand what it does, but I\'m really interesting in an explanation about this pattern. Specifically:

I used this pattern in a few projects, (this snipped of code is from CodeCampServer), I understand what it does, but I'm really interesting in an explanation about this pattern. Specifically:

  1. Why is the double check of _depe开发者_Python百科ndenciesRegistered.
  2. Why to use lock (Lock){}.

Thanks.

public class DependencyRegistrarModule : IHttpModule
{
    private static bool _dependenciesRegistered;
    private static readonly object Lock = new object();

    public void Init(HttpApplication context)
    {
        context.BeginRequest += context_BeginRequest;
    }

    public void Dispose() { }

    private static void context_BeginRequest(object sender, EventArgs e)
    {
        EnsureDependenciesRegistered();
    }

    private static void EnsureDependenciesRegistered()
    {
        if (!_dependenciesRegistered)
        {
            lock (Lock)
            {
                if (!_dependenciesRegistered)
                {
                    new DependencyRegistrar().ConfigureOnStartup();
                    _dependenciesRegistered = true;
                }
            }
        }
    }
}


This is the Double-checked locking pattern.

The lock statement ensures that the code inside the block will not run on two threads simultaneously.
Since a lock statement is somewhat expensive, the code checks whether it's already been initialized before entering the lock.
However, because a different thread might have initialized it just after the outer check, it needs to check again inside the lock.

Note that this is not the best way to do it.


The double-check is because two threads could hit EnsureDependenciesRegistered at the same time, both find it isn't registered, and thus both attempt to get the lock.

lock(Lock) is essentially a form of mutex; only one thread can have the lock - the other must wait until the lock is released (at the end of the lock(...) {...} statement).

So in this scenario, a thread might (although unlikely) have been the second thread into the lock - so each must double-check in case it was the second, and the work has already been done.


It's a matter of performance.

The initial test lets it bail out quickly if the job is already done. At this point it does the potentially expensive lock but it has to check it again as another thread could have already registered it.


The double checked locking pattern is roughly:

you have an operation that you want to conditionally perform once

if (needsToDoSomething) {
   DoSomething();
   needsToDoSomething = false;
}

however, if you're running on two threads, both threads might check the flag, and perform the action, before they both set the flag to false. Therefore, you add a lock.

lock (Lock) {
    if (needsToDoSomething) {
       DoSomething();
       needsToDoSomething = false;
    }
}

however, taking a lock every time you run this code might be slow, so you decide, lets only try to take the lock when we actually need to.

 if (needsToDoSomething)
    lock (Lock) {
        if (needsToDoSomething) {
           DoSomething();
           needsToDoSomething = false;
        }
    }

You can't remove the inner check, because once again, you have the problem that any check performed outside of a lock can possibly turn out to be true twice on two different threads.


The lock prevents two threads from running ConfigureOnStartup(). Between the if (!_dependenciesRegistered) and the point that ConfigureOnStartup() sets _dependenciesRegistered = true, another thread could check if it's registered. In other words:

  1. Thread 1: _dependenciesRegistered == false
  2. Thread 2: _dependenciesRegistered == false
  3. Thread 1: ConfigureOnStartup() / _dependenciesRegistered = true;
  4. Thread 2: Doesn't "see" that it's already registered, so runs ConfigureOnStartup() again.
0

精彩评论

暂无评论...
验证码 换一张
取 消