c# - Considerations when using lock -
i have 1 small confusion. using 1 static variable called status property in c# follows
private static bool status; public static bool status { { return status; } set { status = value; } }
now have started 2 threads separately first thread sets value using property variable status true/false second thread gets value using property variable status.
here in scenario, thought happen
if first thread tries update value of variable status while second thread tries read value of variable status
whether need use lock statement variable status inside property handle thread synchronization or not needed? me clarifying doubt?
from question understand thread 1 produces status value , thread 2 consumes value , work based on that. here there no lock required since reading primitive type thread safe (while reading data other thread wont corrupt that). lock required when have 2 or more writer threads want write data. eg if both thread going set status value lock block required.
Comments
Post a Comment