Is static variable thread-safe? [Resolved]

Posted by krrishbiju-15589 under C# on 8/30/2013 | Points: 10 | Views : 1435 | Status : [Member] | Replies : 3
Hi,
Is static variable thread-safe?
1. if yes..whats the reason?
2. Is any way to change safe..?

Regards
krrish




Responses

Posted by: Bandi on: 8/30/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
It is clearly unsafe because static variables are shared between requests. Move the list to some place that is unique to a particular request, like a page instance field.
If you need to pass state across postbacks use ViewState or some other non-global mechanism.

If you wish to use Thread-safe static variable you just need to decorate that field with the [ThreadStatic] attribute.
Refer the same here....
http://msdn.microsoft.com/en-us/library/system.threadstaticattribute%28VS.71%29.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 8/30/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
refer this link
http://stackoverflow.com/questions/8907612/mspec-how-to-make-static-variables-thread-safe


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 8/31/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
can you mark it as answer if the above posts helps you......
If not yet post us back

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response