Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Static methods and Thread safety

Static methods and Thread safety

Scheduled Pinned Locked Moved C#
question
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abisodun
    wrote on last edited by
    #1

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

    L 1 Reply Last reply
    0
    • A Abisodun

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

      L Offline
      L Offline
      Leslie Sanford
      wrote on last edited by
      #2

      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

      A 2 Replies Last reply
      0
      • L Leslie Sanford

        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

        A Offline
        A Offline
        Abisodun
        wrote on last edited by
        #3

        Cool - thanx :>)

        1 Reply Last reply
        0
        • L Leslie Sanford

          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

          A Offline
          A Offline
          Abisodun
          wrote on last edited by
          #4

          I thought were clear - You meant no instance variable of the class as opposed to an instance declared local to the method. Thanks.

          L 1 Reply Last reply
          0
          • A Abisodun

            I thought were clear - You meant no instance variable of the class as opposed to an instance declared local to the method. Thanks.

            L Offline
            L Offline
            Leslie Sanford
            wrote on last edited by
            #5

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

            A 1 Reply Last reply
            0
            • L Leslie Sanford

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

              A Offline
              A Offline
              Abisodun
              wrote on last edited by
              #6

              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.

              L 1 Reply Last reply
              0
              • A Abisodun

                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.

                L Offline
                L Offline
                Leslie Sanford
                wrote on last edited by
                #7

                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.

                A 1 Reply Last reply
                0
                • L Leslie Sanford

                  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.

                  A Offline
                  A Offline
                  Abisodun
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups