views:

54

answers:

2

Say that I have

public static class M
{
    public static string Concat(string a, string b)
    {
        return N.AddOne(a, b);
    }
    public static string Concat2(string a, string b)
    {
        return SomeThreadSafeMethod(a, b);
    }
}

public static class N
{
 public static string AddOne(string a, string b)
    {
        return a+b+1;
    }
}

Is M.Concat a threadsafe operation? What about M.Concat2? Would it be possible for SomeThreadSafeMethod get call with a different a or b in a multithread context?

The reason I ask this question is I want to have a better understanding of how C# deal with threading, especially for the M.Concat2 case.

Secondly I am working with Asp.net MVC and I am concern that when I call Html.ActionLink or RouteLink with a route with variable something simple such as

Html.ActionLink("Test", "Index", "Test", new { Model.Id, Model.State})

When multiple users hitting the same page, each of them will get a different State. If it's not threadsafe then I might get back a different route than intended.

+2  A: 

The Concat method is absolutely thread safe because it doesn't set any variable. The Concat2 is also thread safe because of the way you are calling it. You only pass Model properties and and you get a different model instance every time. The only issue might be in the SomeThreadSafeMethod method if it sets static variables without locking.

Darin Dimitrov
+2  A: 

Yes, your code is perfectly thread-safe and reentrant.

Anton Gogolev