tags:

views:

147

answers:

1

What is the best way to share a static variable between two threads in .net?
I have summarized my question in following code snippet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace StackOverflow
{

    class ThreadStaticProgram
    {
        static string threadStaticVariable = "";
        static void Main(string[] args)
        {

            Console.WriteLine(" Main Thread Before {0} ", threadStaticVariable);
            threadStaticVariable = " Main Thread ";
            Console.WriteLine(" Main Thread Before For Loop = {0} ", threadStaticVariable);
            Thread[] threads = new Thread[3];
            for (int i = 0; i < 3; i++)
            {
                threads[i] = new Thread(delegate(object j)
                {
                    Console.WriteLine(" Thread{0} before = {1} ", j, threadStaticVariable);
                    threadStaticVariable = " Thread " + j;
                    Console.WriteLine(" Thread{0} after ={1} ", j, threadStaticVariable);
                }
                );
                threads[i].Start(i);
            }
            Array.ForEach(threads, delegate(Thread t) { t.Join(); });
            Console.WriteLine(" Main Thread after For Loop = {0} ", threadStaticVariable);
            Console.ReadLine();
        }
    }
}

QuestionII - What is Thread-Local Storage?

A: 

It depends on the variable. If it's a type that can be set atomically mark it volatile and/or use the Interlocked class to change it's value.

If it's a single reference that's changing, you can use Interlocked.Exchange to do this atomically.

Otherwise, you'll probably want to use some form of synchronization. A private static object on which you can wrap with a lock is the most common option, but it really depends. If it's a collection, using ConcurrentQueue<T> and similar removes the need for locks.

Reed Copsey
Thanks for quick reply currently i am using lock keyword but i wanted to know that is any other options available in .net that i can use?
santosh
@santosh: What is the actual type of the variable you're sharing?
Reed Copsey
@santosh, unless you've seen a significant performance problem with locks, I highly recommend sticking with simple lock statements. It's very easy to make mistakes with lock-free synchronization and the kinds of mistakes you can make are often very difficult to diagnose.
Dan Bryant
@Dan: That's true - unless it's something simple to use like storage in a ConcurrentQueue<T>. Even so, locks are often used improperly. @santosh: I'd recommend posting some code - it'll help us give you the best advice.
Reed Copsey
@Reed, I've been bitten enough to become a tad paranoid. About the only non-locking operation I feel confident using is `Interlocked.Increment` for generating auto-incrementing unique IDs. Definitely true that locking is easy to mess up too, particularly when trying to use `Monitor.Pulse`/`Monitor.WaitOne`.
Dan Bryant
@Dan: Just FYI - these are all very easy to "get right" - worth taking a look at: http://msdn.microsoft.com/en-us/library/dd287108.aspx
Reed Copsey