Click here to Skip to main content
15,896,606 members
Home / Discussions / C#
   

C#

 
AnswerRe: Access form objects from a class Pin
Member 9613-Jun-06 12:12
Member 9613-Jun-06 12:12 
AnswerRe: Access form objects from a class Pin
urbane.tiger13-Jun-06 17:00
urbane.tiger13-Jun-06 17:00 
QuestionSystem.String and Thread Safety Pin
Alexander Wiseman13-Jun-06 11:05
Alexander Wiseman13-Jun-06 11:05 
AnswerRe: System.String and Thread Safety Pin
User 665813-Jun-06 11:17
User 665813-Jun-06 11:17 
GeneralRe: System.String and Thread Safety Pin
Alexander Wiseman13-Jun-06 11:28
Alexander Wiseman13-Jun-06 11:28 
GeneralRe: System.String and Thread Safety Pin
Leslie Sanford13-Jun-06 11:40
Leslie Sanford13-Jun-06 11:40 
GeneralRe: System.String and Thread Safety [modified] Pin
Alexander Wiseman13-Jun-06 11:46
Alexander Wiseman13-Jun-06 11:46 
GeneralRe: System.String and Thread Safety [modified] Pin
Leslie Sanford13-Jun-06 11:58
Leslie Sanford13-Jun-06 11:58 
Alexander Wiseman wrote:
Let me just make sure I have this clear with a quick example. Let us say that I have two threads, A and B, both of which have access to String someStr = "My String".

Now let's say that the processor gives a time-slice to thread A, which then reads the string. Then the processor gives a time-slice to thread B, which then changes the string from "My String" to "Your String".

If I understand you correctly, then because strings are immutable, no matter when the processor decides to temporarily stop processing on thread A, nevertheless thread A will still see the value "My String", even though thread B may have "changed" it already. Is that correct?


What you're doing here isn't changing the string. You are changing the reference to the string. When you do this:

string someString = "My String";


And then do this:

someString = "Your String";


You've taken the reference to "My String" and changed it to point (sorry for the C++ nomenclature) to "Your String";

That's not the same as actually changing the string. And as far as I know, changing what a reference, erm, references is not an intrinsically thread safe operation, but I could be wrong.

Changing the actual string would be doing something like this:

string someString = "My String";

// Just making this method up for the example.
someString.Concatenate(" and Your String");

// Output: "My String and Your String"
Console.WriteLine(someString);


Now, the above code isn't possible with an immutable string. Rather the operation for concatenating a string creates a new string:

string someString = "My String";

string anotherString = someString.Concatenate(" and Your String");

// Output: My String"
Console.WriteLine(someString);
// Output: My String and Your String"
Console.WriteLine(anotherString);


Again, I'm not sure if the string class actually has a "Concatenate" method, but the principle is the same. When doing a write operation on a string, a new string is created as a result leaving the original unchanged.

[EDIT]Apparently, changing what an object references is an atomic operation, so it is thread safe.[/EDIT]



-- modified at 18:00 Tuesday 13th June, 2006
AnswerRe: System.String and Thread Safety [modified] Pin
Leslie Sanford13-Jun-06 11:44
Leslie Sanford13-Jun-06 11:44 
GeneralRe: System.String and Thread Safety Pin
Alexander Wiseman13-Jun-06 11:52
Alexander Wiseman13-Jun-06 11:52 
AnswerRe: System.String and Thread Safety Pin
Guffa13-Jun-06 11:46
Guffa13-Jun-06 11:46 
GeneralRe: System.String and Thread Safety Pin
Alexander Wiseman13-Jun-06 11:51
Alexander Wiseman13-Jun-06 11:51 
GeneralRe: System.String and Thread Safety Pin
Leslie Sanford13-Jun-06 12:00
Leslie Sanford13-Jun-06 12:00 
GeneralRe: System.String and Thread Safety Pin
Mr. VB.NET13-Jun-06 12:46
Mr. VB.NET13-Jun-06 12:46 
GeneralRe: System.String and Thread Safety Pin
Judah Gabriel Himango13-Jun-06 13:24
sponsorJudah Gabriel Himango13-Jun-06 13:24 
GeneralRe: System.String and Thread Safety Pin
Leslie Sanford13-Jun-06 13:36
Leslie Sanford13-Jun-06 13:36 
GeneralRe: System.String and Thread Safety Pin
Judah Gabriel Himango13-Jun-06 13:26
sponsorJudah Gabriel Himango13-Jun-06 13:26 
QuestionSharing source code between classes? Pin
Member 9613-Jun-06 10:39
Member 9613-Jun-06 10:39 
AnswerRe: Sharing source code between classes? Pin
gantww13-Jun-06 10:46
gantww13-Jun-06 10:46 
GeneralRe: Sharing source code between classes? Pin
Member 9613-Jun-06 11:38
Member 9613-Jun-06 11:38 
GeneralRe: Sharing source code between classes? Pin
Guffa13-Jun-06 11:50
Guffa13-Jun-06 11:50 
GeneralRe: Sharing source code between classes? Pin
Member 9613-Jun-06 12:11
Member 9613-Jun-06 12:11 
AnswerRe: Sharing source code between classes? Pin
Guffa13-Jun-06 13:50
Guffa13-Jun-06 13:50 
JokeRe: Sharing source code between classes? Pin
led mike13-Jun-06 11:04
led mike13-Jun-06 11:04 
AnswerRe: Sharing source code between classes? Pin
Ed.Poore13-Jun-06 11:27
Ed.Poore13-Jun-06 11:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.