Pregunta

I have a class like the one below:

class Program
    {
        static void Main(string[] args)
        {
            var outputWindow = new OutputWindow();

            var threads = new List<Thread>();

            Action action = () => outputWindow.Display(20);

            for (int i = 0; i < 10; i++)
            {
                var thread = new Thread(() => action()) {Name = "Thread " + i};
                threads.Add(thread);
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }
        }
    }

    public class OutputWindow
    {
        public void Display(int x)
        {
            for (int i = 0; i < x; i++)
            {
                Console.WriteLine(Thread.CurrentThread.Name + " Outputcounter: " + i);
            }
        }
    }

Question is- is this thread safe and will this lead to any race condition on the local variable i inside the display method? Will all the threads increment the value of the variable "i" as expected(which is it increments the value and does not encroch into other threads value of i)

If this is threadsafe, will it be safe to assume that any local variables declared within a method is always thread safe and shared variables are the ones which needs synchronization?

Thanks, -Mike

¿Fue útil?

Solución

Each method invocation will have a separate set of local variables. However, those variables could refer to objects which other methods also use. For example:

public void AppendSomething(StringBuilder builder)
{
    builder.Append("Something");
}

Here builder is still a local variable (parameters are local variables) and each invocation of AppendSomething will have an independent variable - but you could call the method from multiple threads using the same StringBuilder reference, so that method is not thread-safe. (As StringBuilder isn't thread-safe.)

Otros consejos

As long as your data is local, yes, it's thread-safe. If it references data outside of that method, then it's not.

In your example, i is not affected by multiple threads (because you create the variable, set a local value and just increment the value).

If i was referencing something outside of the method (for example a file) then it wouldn't be thread-safe, because you could be referring to the same file on different threads.

Local variables are thread safe within a method as long as you don't pass them to other thread. If you do, then it depends.

For example, in your code all the threads you create have shared access to outputWindow and threads. If the objects are not thread safe and you call them from multiple threads then you may experience problems. For example, if each thread were to try and remove itself from threads when it finishes then you'd have a problem as List<> isn't thread safe for read/write.

All your threads are using the same OutputWindow instance. If the call to Display mutated the state of the object then need ensure that mutation was threadsafe, otherwise you'd have a race condition.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top