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. Is This Method Thread Safe ?

Is This Method Thread Safe ?

Scheduled Pinned Locked Moved C#
tutorialxmlhelpquestion
11 Posts 5 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.
  • X Offline
    X Offline
    X2040
    wrote on last edited by
    #1

    Hello All Am developing a MultiThreading Application and I have searched the internet for guide lines how to write a thread safe methods. I found some conflicts and I would like if any of you help me to figure out the correct solution. Below is Example of a Method That I need you to Evaluate if it is thread safe or not and Why. public static int Search(string xml, char item) { return xml.IndexOf(item); }

    A L P L 4 Replies Last reply
    0
    • X X2040

      Hello All Am developing a MultiThreading Application and I have searched the internet for guide lines how to write a thread safe methods. I found some conflicts and I would like if any of you help me to figure out the correct solution. Below is Example of a Method That I need you to Evaluate if it is thread safe or not and Why. public static int Search(string xml, char item) { return xml.IndexOf(item); }

      A Offline
      A Offline
      Anthony Mushrow
      wrote on last edited by
      #2

      Well calling IndexOf will invariably run through a loop at some point looking for item Is this thread safe?

      public static int Search(string xml, char item)
      {
      for(int i=0; i<xml.Length; i++)
      {
      if(xml[i] == item)
      return i;
      }

      return -1;
      }

      EDIT: :doh: As others have said strings are of course, immutable. I deserve a kick for that one.

      My current favourite quote is: Punch them in the face, see what happens!

      -SK Genius

      modified on Wednesday, June 2, 2010 10:20 AM

      L 1 Reply Last reply
      0
      • X X2040

        Hello All Am developing a MultiThreading Application and I have searched the internet for guide lines how to write a thread safe methods. I found some conflicts and I would like if any of you help me to figure out the correct solution. Below is Example of a Method That I need you to Evaluate if it is thread safe or not and Why. public static int Search(string xml, char item) { return xml.IndexOf(item); }

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        yes, that method is thread-safe: its results only depend on input parameters, and both string and char are immutable, so there is nothing that can disturb the method; also the method is not changing the outside world, so it isn't disturbing anything itself. As soon as you start referring to other data, such as class members, things may change though. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        1 Reply Last reply
        0
        • X X2040

          Hello All Am developing a MultiThreading Application and I have searched the internet for guide lines how to write a thread safe methods. I found some conflicts and I would like if any of you help me to figure out the correct solution. Below is Example of a Method That I need you to Evaluate if it is thread safe or not and Why. public static int Search(string xml, char item) { return xml.IndexOf(item); }

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          As you aren't operating on a member variable in this example, and you have a nice self contained method where you have immutable data, this is perfectly thread safe. In other words, there is no chance that another thread can change the value of xml or item and affect the outcome. Suppose that you changed it so that xml was a member property of type XMLElement, and you passed item in - then you could have a none thread-safe method because you could affect XMLElement before you actually performed the operation on the XML.

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          • A Anthony Mushrow

            Well calling IndexOf will invariably run through a loop at some point looking for item Is this thread safe?

            public static int Search(string xml, char item)
            {
            for(int i=0; i<xml.Length; i++)
            {
            if(xml[i] == item)
            return i;
            }

            return -1;
            }

            EDIT: :doh: As others have said strings are of course, immutable. I deserve a kick for that one.

            My current favourite quote is: Punch them in the face, see what happens!

            -SK Genius

            modified on Wednesday, June 2, 2010 10:20 AM

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            IMO everything you said was correct, however it did not provide an answer, it merely rephrased the question. I will not provide a kick you say you deserved. :laugh:

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read formatted code with indentation, so please use PRE tags for code snippets.


            I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


            A 1 Reply Last reply
            0
            • L Luc Pattyn

              IMO everything you said was correct, however it did not provide an answer, it merely rephrased the question. I will not provide a kick you say you deserved. :laugh:

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read formatted code with indentation, so please use PRE tags for code snippets.


              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


              A Offline
              A Offline
              Anthony Mushrow
              wrote on last edited by
              #6

              I but I was trying to point out that if the string changed while your where looping you would have a problem. But of course in c# when you pass a string it's a whole new object. Been working with C++ for too long now it seems, where everything has pointers or pointers to pointers. In one case a had a pointer to a pointer to a pointer, pointy.

              My current favourite quote is: Punch them in the face, see what happens!

              -SK Genius

              L 1 Reply Last reply
              0
              • A Anthony Mushrow

                I but I was trying to point out that if the string changed while your where looping you would have a problem. But of course in c# when you pass a string it's a whole new object. Been working with C++ for too long now it seems, where everything has pointers or pointers to pointers. In one case a had a pointer to a pointer to a pointer, pointy.

                My current favourite quote is: Punch them in the face, see what happens!

                -SK Genius

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                SK Genius wrote:

                when you pass a string it's a whole new object.

                Now that deserves a kick. A string parameter is not a new object, in fact it is a pointer (they like to call it a reference), just as it is in C/C++/Java. However it points to an immutable object, something the compiler will not allow you to modify, only replace. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read formatted code with indentation, so please use PRE tags for code snippets.


                I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                A 1 Reply Last reply
                0
                • X X2040

                  Hello All Am developing a MultiThreading Application and I have searched the internet for guide lines how to write a thread safe methods. I found some conflicts and I would like if any of you help me to figure out the correct solution. Below is Example of a Method That I need you to Evaluate if it is thread safe or not and Why. public static int Search(string xml, char item) { return xml.IndexOf(item); }

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Not if you change the string with unsafe code. Otherwise the result will of course be correct for the (old) value of the xml parameter, but not necessarily for "something else" (I mean, if you passed in this.Xml or something like that, the result (when it is returned) may have nothing to do with the current value of that field)

                  X 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    SK Genius wrote:

                    when you pass a string it's a whole new object.

                    Now that deserves a kick. A string parameter is not a new object, in fact it is a pointer (they like to call it a reference), just as it is in C/C++/Java. However it points to an immutable object, something the compiler will not allow you to modify, only replace. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read formatted code with indentation, so please use PRE tags for code snippets.


                    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                    A Offline
                    A Offline
                    Anthony Mushrow
                    wrote on last edited by
                    #9

                    Indeed, right you are. Glad too see I'm earning my Kicks. I know that (almost) everything is passed by reference so you get the actual string but anybody who modifies or replaces it is then pointing to a new object, leaving the old one intact until GC will eventually devour its soul, right?

                    My current favourite quote is: Punch them in the face, see what happens!

                    -SK Genius

                    1 Reply Last reply
                    0
                    • L Lost User

                      Not if you change the string with unsafe code. Otherwise the result will of course be correct for the (old) value of the xml parameter, but not necessarily for "something else" (I mean, if you passed in this.Xml or something like that, the result (when it is returned) may have nothing to do with the current value of that field)

                      X Offline
                      X Offline
                      X2040
                      wrote on last edited by
                      #10

                      Some articles say that if you pass variables to your method that are not immutable or reference variable this may make the method not Thread Safe. Does any one has more details about this point if it is correct or wrong and why?

                      L 1 Reply Last reply
                      0
                      • X X2040

                        Some articles say that if you pass variables to your method that are not immutable or reference variable this may make the method not Thread Safe. Does any one has more details about this point if it is correct or wrong and why?

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        What exactly do you mean?

                        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