Is This Method Thread Safe ?
-
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); }
-
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); }
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
-
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); }
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).
-
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); }
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.
-
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
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).
-
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).
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
-
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
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).
-
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); }
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 inthis.Xml
or something like that, the result (when it is returned) may have nothing to do with the current value of that field) -
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).
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
-
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 inthis.Xml
or something like that, the result (when it is returned) may have nothing to do with the current value of that field) -
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?