0

I have a static DataLibrary class that implements a singleton pattern.

        public static FacilityRepository FacilRepo
        {
            get
            {
                if (_facilRepo == null)
                {
                    _facilRepo = new FacilityRepository(Authenticated.UserId);
                    if (Authenticated.FacKey.Length > 0)
                    {
                        foreach (var fac in _facilRepo)
                            fac.IsSelected = (fac.FacilityKey == Authenticated.FacKey);                        
                    }
                }
                return _facilRepo;
            }
        }

private static FacilityRepository _facilRepo;

When I access this from different threads using Task.Factory.StartNew the FacilityReposity gets recreated multiple times how can I avoid this.

1
  • If you are creating many new threads at the same time and they are all trying to access the FacilRepo property.. it may be because there is no locking around the property. Take a look at en.csharp-online.net/… (right at the bottom) Commented Mar 17, 2011 at 15:03

3 Answers 3

9

I don't think you've actually got a thread-local variable here - you've just got a race condition because you're not implementing the singleton pattern properly.

I have a page about the singleton pattern which gives some better options. (In particular, as you're using the TPL you must be using .NET 4, so the Lazy<T> option is definitely a contender.)

Sign up to request clarification or add additional context in comments.

1 Comment

excellent Article!!!! Thank you . Ill accept your answer as soon as it lets me 4 more minutes.
2

This Article by Jon Skeet might be helpful: Implementing the Singleton Pattern in C#

These questions might be helpful too:

Comments

0

This would happen if multiple threads access your property for the first time, before _facilRepo is initialised. You have to lock the initialisation code like this:

private static object _facilRepoLock = new object();
public static FacilityRepository FacilRepo
{
    get
    {
        if (_facilRepo == null)
        {
            lock (_facilRepoLock)
            {
                if (_facilRepo == null)
                {
                    _facilRepo = new FacilityRepository(Authenticated.UserId);
                    if (Authenticated.FacKey.Length > 0)
                    {
                        foreach (var fac in _facilRepo)
                            fac.IsSelected = (fac.FacilityKey == Authenticated.FacKey);
                    }
                }
            }
        }
        return _facilRepo;
    }
}

private static FacilityRepository _facilRepo;

2 Comments

Without the variable being volatile, I'm not sure that's genuinely thread-safe. There are simpler ways of achieving thread-safety than using double-checked locking.
Ahh, interesting.. I didn't know that before.. Your article is an eye-opener :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.