1

As I am working on multi-threaded application (.Net Windows service) with .Net, I need to make sure the following things.

  • I am calling each task (business scenarios) on each different thread with Delegate BeginInvoke patterns. (Which means that a instance method of new object I am calling with each different thread in iteration of loop for 1000 clients)
  • I have one scenario in the application that, for the first time (When my window service starts), I want to set flag somewhere in the application. (May be by C# static fields)
  • I want to make sure that, once the first thread Update the value (of static field), then all other rest of the thread MUST only use the last value which is set by 1st thread.

So, basically, I want to update the value (of C# static field) for the first time only (When my windows service start), and want my all other thread should use the latest value set by first thread.

So, I want to update the value ONLY ONCE, then rest other thread should use only that value.

Would anybody please tell me that How would I achieve this?

Thanks in advance!

nunu
  • 3,184
  • 10
  • 43
  • 58

4 Answers4

2

You can use Lazy<T> for this

static Lazy<string> _singleton = new Lazy<string>(() =>
    {
        return new WebClient().DownloadString("http://www.stackoverflow.com");
    }, true);

Second parameter (true) is "isThreadSafe".

Now you can get the value in many threads, many times using _singleton.Value;

I4V
  • 34,891
  • 6
  • 67
  • 79
  • I really like `Lazy`... it's pretty handy for situations like this. It's not necessary to supply the `isThreadSafe` parameter. When omitted, `Lazy` defaults to the safest thread-safety mode. – spender May 23 '13 at 19:16
0

Seem like Threadsafe Singleton will do the job. The below code example use double-check locking

This will ensure you that the creation of the singleton is 1 time and thread safe

public sealed class Singleton
{

   private static volatile Singleton singleton = null;
   private static readonly object singletonLock = new object();

   private Singleton() {//place your code here}

   public static Singleton GetInstance()
   {
       if (singleton == null)
       {
           lock (singletonLock)
           {
               if (singleton == null)
              {
                  singleton = new Singleton();
              }
           }
        }

        return singleton;
   }
}

Edit: Add volatile due to members comment.

Mzf
  • 5,210
  • 2
  • 24
  • 37
  • I thought double-check locking was still broken: http://stackoverflow.com/questions/394898/double-checked-locking-in-net –  May 23 '13 at 18:37
  • ebyrob is correct. You might be able to fix this by using the volatile keyword or memory barriers, but it's probably better to avoid double-checked locking all together. – Special Touch May 23 '13 at 18:40
  • You might want to actually return the `singleton` you created :P – Moo-Juice May 23 '13 at 18:52
0

Why not use this type of singleton. The static constructor is called ONLY once. And since static initializations are part of the static constructor, the initialization is also called only once. The keyword volatile is not required, since the _instance field can not have any other value after construction.

public sealed class Singleton
{
    private static Singleton _instance= new Singleton();

    private Singleton() 
    {
    }

    public static Singleton Instance
    {
        get
        {
            return _instance;
        }
    }
}
Martin Mulder
  • 12,642
  • 3
  • 25
  • 54
0

As I4V suggested - use Lazy<T> and for simplicity I suggest hide it under a read-only property:

static readonly Lazy<T> t = new Lazy<T>(() => new T());
static public T Value { get { return t.Value; }}
Tadas Šukys
  • 4,140
  • 4
  • 27
  • 32