Are local variables threadsafe?

落爺英雄遲暮 提交于 2019-12-22 05:53:14

问题


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


回答1:


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.)




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/21183482/are-local-variables-threadsafe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!