0

I was working with lock statements and started reading a little more about it. I know the basics on how it works and when to use it, but I tried to understand a little under the hood of it's work and came with some questions I need some help to understand.

1 - Microsoft docs say we should use a reference type. Can we use any reference type? I usually only see people using a new object() for locks but would it make any practical difference of using for example new List<int>() as the lock object? I understand it might be a bad practice but would it cause any difference in the app behavior?

2 - What does the lock do to the object? When we use an object to be locked, does the lock operation ever change the object binary value? Does it change the pointer value? Since is a reference type, does the lock use the memory pointer for something?

I found this answer on StackOverflow but it is not exactly what I was searching for, the question is similar but the answers tells more that the lock statement is same as using Monitor class and main behavior, but then what does the Monitor do to the lock object?

I'd like to understand what happens to the program, what is the processor doing when acquiring a lock also what does change in the program memory.

FabioStein
  • 750
  • 7
  • 23
  • 1
    This is explaining it a little bit better(since `lock` is actually a `Monitor.Enter`): https://stackoverflow.com/questions/6770815/how-does-system-threading-monitor-enter-work – Tim Schmelter Mar 17 '23 at 15:47
  • 2
    Be aware that when you programming something rely on the description of that thing. Here whats in the documentation of lock. Knowing about the implementation is nice (i like to know those things too) but its just one implementation and implementations change, have changed, will change and there could be multiple in parallel. So don't rely on the knowledge you may gain here rely on the documentation primarily. And the guidelines in the doc are pretty clear what to use and according to that `new List()` is no problem. – Ralf Mar 17 '23 at 16:01

2 Answers2

1

A fundamental principle of the .NET framework is that as long as any copy of a reference to an object exists anywhere in the universe, nothing in the universe that isn't a copy of a reference to that same object will compare equal to it.

The only aspect of an object reference or its associated object which is relevant to SyncBlock or related constructs is whether it is a copy of a reference to any of the same objects as any presently-active SyncBlocks. Although the mechanisms used to determine this exploit a little bit of spare storage from the headers of the objects in question, what's important is that identity of the objects in question, rather than anything they might contain.

supercat
  • 77,689
  • 9
  • 166
  • 211
0

The first time you use lock on an object, a SyncBlock is initialized and associated with that instance. You can use any reference type including List.

Additional reading including memory layout details:

(.NET 1.1) https://web.archive.org/web/20080919091745/http://msdn.microsoft.com:80/en-us/magazine/cc163791.aspx

What is a "Sync Block" and tips for reducing the count

(newer approach: thin locks) https://devblogs.microsoft.com/premier-developer/managed-object-internals-part-2-object-header-layout-and-the-cost-of-locking/

teapot418
  • 1,239
  • 1
  • 3
  • 9