Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't we lock on a value type?

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.

like image 661
Otiel Avatar asked Nov 25 '11 09:11

Otiel


People also ask

Why should you avoid the lock keyword?

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.

Does a lock block thread?

Lock is not blocking threads. It is locking some instance of an object. And each thread which tries to access it is blocked.

Can you lock a null object C#?

C# doesn't allow locking on a null value.


1 Answers

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.

like image 98
Andrew Barber Avatar answered Oct 17 '22 11:10

Andrew Barber