|
Thanks but you're late (I don't use nHibernate no more!)
(But still thanks and voted 5 for ya)
dev
|
|
|
|
|
Hello everyone,
Support I am using str1 = str2 statement, both str1 and str2 are type of string.
Then, suppose str1 is hold by thread 1 and str2 is hold by thread 2. My question is whether operation (for example, tryu to change content of the string, toupper, tolower, replacement) on str1 (from thread 1) and operaton on str2 (from thread 2) are thread safe?
My idea is it is thread safe, since even if str1 and str2 are pointing to the same string instance, but as string is immutable, i.e. each operation which will change the content of the string will make a new string. So, it is thread safe.
Am I correct?
thanks in advance,
George
|
|
|
|
|
A quick check on MSDN[^] would show you that the string class as a whole is thread safe.
|
|
|
|
|
All operations in string class is thread safe?
regards,
George
|
|
|
|
|
Did you not even look at the link I sent you?!
It definitely isn't definatley
|
|
|
|
|
I read it and it mentions all string operations are thread safe. It makes me surprised, just double check with you.
regards,
George
|
|
|
|
|
I'm sure I should be flattered but given that Microsoft wrote the class, I'd probably believe their documentation over anything I have to say.
It definitely isn't definatley
|
|
|
|
|
Thanks moon_stick,
I agree with the conclusion. My question is about why.
I think the reason why all methods of string class are thread safe is because string is immutable, i.e. when the string is modified, a new instance is created. Agree or?
regards,
George
|
|
|
|
|
Immutable objects are inherently thread safe but it's quite possible for an object to be thread safe without being immutable.
I'm done answering questions on this topic now - go and search on google (thread safety) or MSDN if you want more detail.
It definitely isn't definatley
|
|
|
|
|
Thanks moon_stick,
I appreciate your help.
My question is for the root cause why string is thread safe, is it because string is immutable (i.e. every update operation on string will create a new instance.)?
(MSDN does not answer this.)
regards,
George
|
|
|
|
|
As I stated in my last answer, strings are thread safe because strings are immutable and immutable objects are thread safe.
I can't see a valid reason why you'd actually need to know this information anyway (and quite franky, don't care so don't bother telling me).
Now leave me alone before I lose my temper.
It definitely isn't definatley
|
|
|
|
|
Thanks moon_stick,
Sorry for any inconvenience. I like to study internals and find why besides just the rules.
It is good that we find the root reason why string class is thread safe because it is immutable.
regards and have a good weekend,
George
|
|
|
|
|
Im surprised that after being told coutless times to read the docs before asking a question you still don't bother. However this does not change the reality that you still don't.
Do you see how this applies to the current situation?
|
|
|
|
|
Thanks J4amieC,
I agree with the conclusion. My question is about why.
I think the reason why all methods of string class are thread safe is because string is immutable, i.e. when the string is modified, a new instance is created. Agree or?
regards,
George
|
|
|
|
|
moon_stick wrote: Did you not even look at the link I sent you?!
Are you not familiar with George? Yes, he is an odd one, although not sure "odd" does him justice. I believe he has some crazy job that pays him by the number of internet forum posts he makes.
moon_stick wrote: I'm sure I should be flattered but given that Microsoft wrote the class, I'd probably believe their documentation over anything I have to say.
Do not expect George to understand even the simplest things from any documentation. Oh and, good luck.
led mike
|
|
|
|
|
led mike wrote: Are you not familiar with George?
That was my first encounter - I think I get the idea already...!!
It definitely isn't definatley
|
|
|
|
|
|
|
|
Thanks Abhijit,
It talks about the general thread safety rule, not about my specific question about thread safety for string.
Any new ideas or comments?
regards,
George
|
|
|
|
|
George_George wrote: It talks about the general thread safety rule
Yes, That true. but it could also clear your doubts . Any way hope now your have got your answer.
cheers,
Abhijit
|
|
|
|
|
Thanks Abhijit,
I think all string operations are thread-safe. Since when we modify a string, a new string instance will be created. Do you think it is correct?
regards,
George
|
|
|
|
|
George_George wrote: Do you think it is correct?
Yes !!!
cheers,
Abhijit
CodeProject.Com MVP
|
|
|
|
|
Thanks Abhijit MVP!
regards,
George
|
|
|
|
|
Thats 2 more "read the docs" answers for you. That makes your tally, what, like a couple of thousand now?
|
|
|
|