Are static members thread safe?

Posted by Ray.chayan under C# on 7/13/2010 | Views : 3383 | Status : [Member] | Replies : 2
Are the static members are thread safe? I mean if we access any static member from two different thread then what will happen?




Responses

Posted by: Raja on: 7/13/2010 [Member] Starter

Up
0
Down
Thank you for question here.

If you are going to do multi threading, better not to use static members. They are not thread safe.

Static members or variables share the same instance through out the application so the value created by one thread will be used by another and you will get undesired behavior.

To develop a thread safe application, always use instance variables.

Hope this will help.

Thanks

Regards,
Raja, USA

Ray.chayan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ray.chayan on: 7/14/2010 [Member] Starter

Up
0
Down
Hi Raja, Thanks for your answer . Basically my concept about staic class and class members are not clear.
You say that for the two different threads static members are not safe. But if two different users access the same static member in the same application then is it safe? I mean updation of data from 1 member reflects to the another member?

Ray.chayan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response