F
Fred West
I have a class with a private Int32 data member that gets modified from
multiple background threads. To synchronize these modifications I use the
lock statement:
Int32 count = 0;
object countLock = new object();
....
lock (countLock) count++;
....
lock(countLock) count = newValue;
My question is that when I wish to read this value (from yet another thread)
do I need to wrap it in a lock statement. For example, the class has the
following property:
public Int32 Count{get{return count;}}
The reading of the value is done extensively and I do not wish to have the
overhead of the lock statement. Can a thread lose it's turn in the middle of
a lock statement? If so, is it possible that the Int32 field is only
partially updated with it's new value and when I go to read it? What if my
field was an Int64?
Thanks,
- Fred
multiple background threads. To synchronize these modifications I use the
lock statement:
Int32 count = 0;
object countLock = new object();
....
lock (countLock) count++;
....
lock(countLock) count = newValue;
My question is that when I wish to read this value (from yet another thread)
do I need to wrap it in a lock statement. For example, the class has the
following property:
public Int32 Count{get{return count;}}
The reading of the value is done extensively and I do not wish to have the
overhead of the lock statement. Can a thread lose it's turn in the middle of
a lock statement? If so, is it possible that the Int32 field is only
partially updated with it's new value and when I go to read it? What if my
field was an Int64?
Thanks,
- Fred