开发者

Performing one time initialization in multithreaded C# programs

开发者 https://www.devze.com 2023-01-11 16:37 出处:网络
Is the snippet below \"safe\" for performing some initialization once in a multithreaded program? I\'m a bit worried that boxing/unboxing might cause some problem...

Is the snippet below "safe" for performing some initialization once in a multithreaded program?

I'm a bit worried that boxing/unboxing might cause some problem...

private static object initialized = false;

public static void Initialize()
{
  lock (initialized)
  开发者_开发百科{
    if ((bool)initialized == false)
    {
      DoInitialization();
      initialized = true;
    }
  }
}

Note that I can't just do the initialization from Main or some other single threaded context.


You are right - that's not going to work because you are reassigning the object you are locking on.

You can do this with two seperate members - an object to lock on that is assigned only once, and then a boolean that you can read or assign to (after you have locked).

You may also want to consider if a singleton would be appropriate here.


Try a double-checked locking.

    private static volatile bool initialized = false;
    private static object syncObject = new object();
    public static void Initialize() {
        if (!initialized) {
            lock (syncObject) {
                if (!initialized) {
                    DoInitialization();
                    initialized = true;
                }
            }
        }
    }


A few options:

  1. lock on a once-assigned reference-type, as Mark has mentioned.
  2. Initialize on the static-constructor, which is guaranteed to only run once. Con: Hard to control when you want the initialization to occur.
  3. If possible, the Lazy class, which is thread-safe.

All 3 methods can also be used to create Singletons.

0

精彩评论

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