0

I know I can't use lock (SyncLock) on a value type, as explained in this article:

http://msdn.microsoft.com/msdnmag/issues/03/01/NET/

But my question is, does this mean I don't need to? or do I need to wrap the Boolean within an object, so it can be locked? If the answer is no, I don't need to lock it, what happens when two threads try to change the value at the same time? In the application I am currently working on, it is possible two threads could try to set it to both true and false at the same time.

Ben
  • 165
  • 2
  • 14

1 Answers1

0

The Boolean, in itself, doesn't need a lock.
If it changes under your feet, you may get the old value or the new value, and it's OK.
Normally, however, a variable doesn't exist by itself. It's value is related to the value of others, and you need to protect the whole group.
For example, you may have a list, and a Boolean indication if it's full. You need a lock that covers both of them, to keep the two consistent.

ugoren
  • 16,023
  • 3
  • 35
  • 65