I was trying to lock
a Boolean
variable when I encountered the following error :
'bool' is not a reference type as required by the lock statement
It seems that only reference types are allowed in lock
statements, but I'm not sure I understand why.
Andreas is stating in his comment:
When [a value type] object is passed from one thread to the other, a copy is made, so the threads end up working on 2 different objects, which is safe.
Is it true? Does that mean that when I do the following, I am in fact modifying two different x
in the xToTrue
and the xToFalse
method?
public static class Program { public static Boolean x = false; [STAThread] static void Main(string[] args) { var t = new Thread(() => xToTrue()); t.Start(); // ... xToFalse(); } private static void xToTrue() { Program.x = true; } private static void xToFalse() { Program.x = false; } }
(this code alone is clearly useless in its state, it is only for the example)
P.S: I know about this question on How to properly lock a value type. My question is not related to the how but to the why.
Avoid using 'lock keyword' on string object String object: Avoid using lock statements on string objects, because the interned strings are essentially global in nature and may be blocked by other threads without your knowledge, which can cause a deadlock.
Lock is not blocking threads. It is locking some instance of an object. And each thread which tries to access it is blocked.
C# doesn't allow locking on a null value.
Just a wild guess here...
but if the compiler let you lock on a value type, you would end up locking nothing at all... because each time you passed the value type to the lock
, you would be passing a boxed copy of it; a different boxed copy. So the locks would be as if they were entirely different objects. (since, they actually are)
Remember that when you pass a value type for a parameter of type object
, it gets boxed (wrapped) into a reference type. This makes it a brand-new object each time this happens.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With