c# - Threading behavior of methods of a class object declared as static member of another class -
recently colleague of mine came piece of code , asked opinion regarding thread safety of code. below example illustrates same scenario code.
public class classa { public int doworka() { //some logic } } public class classb { public static classa obja = new classa(); } public class classc { int doworkc () { return classb.obja.doworka(); } } now if classb.obja.doworka() called simulatenously in different instances of different classes classc, classd etc there threading or 'overlap' issues ? should obja converted instance member ?
since obja static there single instance of classa. means threads access method doworka() on same instance, not mean method call thread-safe - entirely depends on implementation of doworka().
you still need appropriate logic in doworka protect problems can arise concurrent access, e.g. use of locking or thread-safe collections etc.
Comments
Post a Comment