Information Technology Consulting Service

www.katarincic.com


Welcome Guest Search | Active Topics | Members | Log In

C# : What if a static method is called from multiple threads? Options · View
scott
Posted: Thursday, October 24, 2019 9:44:48 AM
Rank: Administration
Groups: Member

Joined: 3/24/2008
Posts: 2,728
Points: 5,360
Location: South Florida USA
C# : What if a static method is called from multiple threads?

https://stackoverflow.com/questions/3037637/c-sharp-what-if-a-static-method-is-called-from-multiple-threads


Quote:

Variables declared inside methods (with the possible exception of "captured" variables) are isolated, so you won't get any inherent problems; however, if your static method accesses any shared state, all bets are off.

Examples of shared-state would be:

static fields
objects accessed from a common cache (non-serialized)
data obtained via the input parameters (and state on those objects), if it is possible that multiple threads are touching the same object(s)
If you have shared state, you must either:

take care not to mutate the state once it can be shared (better: use immutable objects to represent state, and take a snapshot of the state into a local variable - i.e. rather than reference whatever.SomeData repeatedly, you read whatever.SomeData once into a local variable, and then just use the variable - note that this only helps for immutable state!)
synchronize access to the data (all threads must synchronize) - either mutually exclusive or (more granular) reader/writer


Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.6 (NET v2.0) - 11/14/2007
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 0.046 seconds.