Static methods and Thread safety
-
Anyone confirm this is thread safe please? internal static List GetList(string xmlString) { List list = new List(); BuildList(xmlString, list); return list; } private static void BuildList(string xmlString, List list) { string someXml = ModifyXml(xmlString); list.Add(GetString(someXml)); if (GetBool(someXml)) BuildList(someXml, list); } Thanks much..
-
Anyone confirm this is thread safe please? internal static List GetList(string xmlString) { List list = new List(); BuildList(xmlString, list); return list; } private static void BuildList(string xmlString, List list) { string someXml = ModifyXml(xmlString); list.Add(GetString(someXml)); if (GetBool(someXml)) BuildList(someXml, list); } Thanks much..
Looks thread safe to me. All of the methods you're calling are static; no instance objects are involved. Looks reentrant. I think you're safe. EDIT-> When I say "instance objects," I'm refering to objects that persist at the instance level of a class. That is to say that none of the objects that I see used in the static methods persist at that level. So it looks safe. -- modified at 14:22 Tuesday 6th March, 2007
-
Looks thread safe to me. All of the methods you're calling are static; no instance objects are involved. Looks reentrant. I think you're safe. EDIT-> When I say "instance objects," I'm refering to objects that persist at the instance level of a class. That is to say that none of the objects that I see used in the static methods persist at that level. So it looks safe. -- modified at 14:22 Tuesday 6th March, 2007
-
Looks thread safe to me. All of the methods you're calling are static; no instance objects are involved. Looks reentrant. I think you're safe. EDIT-> When I say "instance objects," I'm refering to objects that persist at the instance level of a class. That is to say that none of the objects that I see used in the static methods persist at that level. So it looks safe. -- modified at 14:22 Tuesday 6th March, 2007
-
I thought were clear - You meant no instance variable of the class as opposed to an instance declared local to the method. Thanks.
Abisodun wrote:
I thought were clear - You meant no instance variable of the class as opposed to an instance declared local to the method.
Exactly. :)
-
Abisodun wrote:
I thought were clear - You meant no instance variable of the class as opposed to an instance declared local to the method.
Exactly. :)
Your statement's been bugging me now I realize why. This code will cause a compilation error: class MyClass { internal string stringVariable = string.Empty; internal static void MyFunction() { stringVariable += "a"; } } Did you mean static variable of the class? Instance variable always refers to a non static variable. i.e. class MyClass { internal string stringVariable = string.Empty; internal static void MyFunction() { MyClass myClass = new MyClass(); myClass.stringVariable += "a"; } } which should not be a problem in a static method of the same class.
-
Your statement's been bugging me now I realize why. This code will cause a compilation error: class MyClass { internal string stringVariable = string.Empty; internal static void MyFunction() { stringVariable += "a"; } } Did you mean static variable of the class? Instance variable always refers to a non static variable. i.e. class MyClass { internal string stringVariable = string.Empty; internal static void MyFunction() { MyClass myClass = new MyClass(); myClass.stringVariable += "a"; } } which should not be a problem in a static method of the same class.
Abisodun wrote:
Did you mean static variable of the class? Instance variable always refers to a non static variable.
Yeah, I should have just said that since the methods are static, it's thread safe. I confused the issue by mentioning instance objects. The instance variables of a class can't be accessed from a static method, so there was no need to bring them into the discussion. The main thing to watch out for with static methods and thread safety are objects that are passed into the method. Something like this:
public static void DoSomething(SomeObject obj)
{
obj.Change();
}This isn't tread safe. The reason being that two threads could call this method at the same time (relatively speaking), and the "Change" method may be performing some thread unsafe operation. The example you gave in your original post didn't do this, so it's thread safe. But also, if an object is a class level variable, you still want to be careful:
public class MyClass
{
private static SomeObject obj = new SomeObject();public static void DoSomething() { obj.Change(); }
}
This isn't thread safe for the same reason the first example isn't thread safe. In your original post, all of the objects that were used were created within the static method. This means that if two threads called the method at the same time, the objects each thread uses is unique to the thread. So your original example is thread safe. Does this help? Hopefully, I haven't made it more confusing.
-
Abisodun wrote:
Did you mean static variable of the class? Instance variable always refers to a non static variable.
Yeah, I should have just said that since the methods are static, it's thread safe. I confused the issue by mentioning instance objects. The instance variables of a class can't be accessed from a static method, so there was no need to bring them into the discussion. The main thing to watch out for with static methods and thread safety are objects that are passed into the method. Something like this:
public static void DoSomething(SomeObject obj)
{
obj.Change();
}This isn't tread safe. The reason being that two threads could call this method at the same time (relatively speaking), and the "Change" method may be performing some thread unsafe operation. The example you gave in your original post didn't do this, so it's thread safe. But also, if an object is a class level variable, you still want to be careful:
public class MyClass
{
private static SomeObject obj = new SomeObject();public static void DoSomething() { obj.Change(); }
}
This isn't thread safe for the same reason the first example isn't thread safe. In your original post, all of the objects that were used were created within the static method. This means that if two threads called the method at the same time, the objects each thread uses is unique to the thread. So your original example is thread safe. Does this help? Hopefully, I haven't made it more confusing.
No it's been very helpful. I was really only concerned about "List list" being passed between static methods and recursively. But each new call stack starting with GetList in the first example should have it's own instance of "List list". I appreciate your help.